Java class inheritance, properties and methods of the scope of the four modifiers, the final keyword, Java's three major features of the 2: encapsulation and polymorphism, as well as a polymorphic design pattern, template method mode

Source: Internet
Author: User
Tags modifiers

(i) Inheritance in Java:

   About inheritance, in Java class inheritance can only be single inheritance, not as flexible as c+++, can inherit more, the consequence of multiple inheritance is a variety of relationship chaos, equivalent to a child has 2 mother, social relations complex, not conducive to the development and maintenance of the late stages of the program, all Java class is only single inheritance, Through the interface to achieve multiple inheritance, for the interface, can be multiple inheritance, for example: A interface can inherit both interface B and interface C, but one thing to note is that the implementation of interface A class must implement interfaces A, B, c all the abstract methods, the interface can be more inheritance, because the interface structure is relatively simple.

Inheritance is implemented by the extends keyword, the default parent class being the object class (root cause).

(ii) Four modifiers (public protected default friendly private)

   1. For public-decorated properties and methods: No matter which class in the package can be used, the affirmation between the different projects is not possible.

2. For protected-modified properties and methods:   first, it can be used in a class of the same package, or in a subclass of another package, but one thing to note is that the property or method cannot be used in the main () method in the subclass

  3. For friendly-modified properties and methods: can only be used in the same package

  4. For the properties and methods of the private adornment: can only be used in this class

(iii) Final modifier

  1. The class that is final modified: cannot be inherited

2. Final Modified variables:   is a constant and cannot be modified

   3. The final modified method: subclasses of this class cannot override this method, but you can overload the method in this class

   4. Note: For example, in the main () method: The code is as follows

      

    the effect of running :

Description: In front of the reference with the FINA modifier, just let A's space address cannot be changed, you can modify the value of the array in a point

(iv) Encapsulation

   The first thing is to use the private modifier, to modify the class's member variables, can only be used in this class, through the set, get methods to provide access to the external portal

Benefits: 1. Access to data only by means of the specified method

2. Hide the specific implementation details of the class

3. Easy to modify the implementation

4. Easy to join control statements

(v) Polymorphism:

polymorphic constituent conditions: 1. To have class inheritance

2. To have a method of rewriting

3. A reference to the parent class object to the child class

Look at the code:

  The effect of running:

   Benefits of Polymorphism:

    

(vi) Template method mode

first, define an abstract template

  

  The idea is to order the hotel to make the final payment.

Write a subclass that implements it: (in writing the Main method)

  

To see how the results work:

Note: This is the template method pattern, before there must be a lot of people wonder, this method I did not call, why run, that is the reason, also called the hook function (hook or callback callback), the specific implementation of the subclass, the parent class to define the template well

Java class inheritance, properties and methods of the scope of the four modifiers, the final keyword, Java's three major features of the 2: encapsulation and polymorphism, as well as a polymorphic design pattern, template method mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.