Java: encapsulation, inheritance, and Polymorphism

Source: Internet
Author: User

Java Encapsulation

Encapsulation. The connection between an object and the outside world should be made public through a unified interface and hidden. (The attributes of an object should be hidden.) an object is transparent internally, that is, the transparency inside the object is distinguished from the hidden features. This transparency is hidden.

(Encapsulated attribute) the default value of the access permission for class attributes in Java is not private. To hide this attribute or method, you can add a private modifier, to restrict access only within the class.

For private attributes in the class, you must provide a pair of methods (getxxx (), setxxx () to access private attributes to ensure the security of operations on private attributes.

Method encapsulation. For method encapsulation, the public is exposed, and the hidden is hidden. A method is a method declaration (Definition), that is, the method can be called only when the parameter and return value are known ), the implementation of the hidden method minimizes the impact of implementation changes on the architecture ..

Encapsulation minimizes the impact of changes in method implementation on the architecture.

Full encapsulation, all the attributes of the class are private, and a pair of methods are provided to access the attributes.

Inheritance in Java

Inheritance is to abstract multiple types of things with common features into a class. This class is the parent class of many types of things. The significance of the parent class is that it can extract the commonalities of multiple types of things.

The extends keyword is used for inheritance in Java, and Java only allows single inheritance, that is, a class can only have one parent class.
In this way, the inheritance relationship is tree-like, reflecting the simplicity of Java.

Subclass can only inherit the attributes and methods that can be accessed in the parent class (in fact, the Private attributes and methods in the parent class will also be inherited, but the subclass cannot be accessed ).

Access Control modifier: (attributes and methods can be modified)
Private modifier, indicating that only the class can be accessed internally.
Default modifier. If the method does not have a modifier, the default modifier is default, indicating that the method can be accessed in the same package. If the parent and child classes are in the same package, the subclass can inherit the corresponding content of the parent class. (Class can be modified)
The protected modifier indicates that the same package can be accessed, and sub-classes of different packages can also be accessed and inherited.
Public modifier, which is public and accessible anywhere. (Class can be modified)
The modifier's permissions are gradually extended from top to bottom.

The significance of inheritance lies in that child classes can develop the features of the parent class based on the parent class, and inheritance can reduce system coupling, that is to say, the link between objects is relaxed, so that the link between multiple classes of objects is replaced by its parent class object.

Note: constructor methods cannot be inherited.

Determining the attributes and methods of the parent class: we need to look at the commonalities between sub-classes from the subclass perspective. When all sub-classes have this attribute, we should consider whether to put it in the parent class, the same is true for methods. methods can be seen as the behavior of objects, while class methods are the common behavior of such objects, therefore, you should also pay attention to the fact that this method is required in all subtypes when the method is fixed, and the method can be overwritten only when the behavior of different types is different.

Override of methods in Java

The method in the subclass that has the same name as the parameter table in the returned type that can be accessed in the parent class (can be inherited to the subclass) will overwrite the method inherited from the parent class.

Note: When method override is required before jdk1.4, the return value, parameter table, and method name of the method must be exactly the same, and the method override in jdk1.5 must be the same, the Return Value of the method covered in the subclass can be a child type of the return value type of the method overwritten in the parent class.

Note: When the subclass method overwrites the parent class method, the method modifier is either the same, or the method modifier in the subclass indicates that the access permission must be wider than the parent class. The private method in the parent class cannot be inherited to the subclass, that is, the subclass does not have polymorphism even if it is overwritten.

The significance of overwriting: The development of methods inherited from the parent class.

Note: If the parent and child classes have attributes with the same name, the sub-classes do not overwrite the attributes of the parent class. In this case, the sub-classes do not overwrite the attributes (Shadow ).

Steps for constructing an object with an inheritance relationship
1. recursively construct the parent class Object
2. Allocate space
3. initialize the instance variables (attributes) of this class)
4. Call the constructor of this class.

Note: The subclass object actually contains the parent class object, that is, the parent class object plus the subclass object, is the complete subclass object instance.
Super keyword
Super () indicates calling the constructor of the parent class in the constructor of the subclass (you can use this method to initialize the attributes of the parent class in the constructor of the subclass), super () it can only appear in the first sentence of the constructor. Super () indicates in the constructor of the Child class which is called to construct the parent class when constructing the parent class.
Super, which indicates the object of a parent class. You can use super to use methods that can be accessed in the parent class (you can define setxxx (), getxxx () in the parent class () to access the private attributes in the parent class). Super can block conflicts between attributes of the same name in the parent and child classes.
Note: When writing a class, you must write a constructor without parameters by default. If the first sentence of a constructor is neither this () nor super, then it will implicitly call the construction method without parameters of its parent class here, that is, the implicit super ().

The principle of less overwrite: the Child class should cover the parent class method as little as possible. If most methods of the parent class are covered, whether there should be an inheritance relationship should be considered.

Polymorphism in Java (on the premise that child classes overwrite the methods of the parent class)

Polymorphism. If the subclass object is regarded as its parent type, the parent type can be of many types.

Polymorphism:

Polymorphism during compilation (method overloading)

Runtime polymorphism (polymorphism)


The compile-time type, that is, the type that can be considered, which is subjective.
The runtime type, that is, the type of the actual object instance, which cannot be changed objectively (also considered as a child type)

For an object, the runtime type is determined when the object is generated and will not be changed. The types during compilation can be different from those during runtime. You can determine the runtime type of the object variable when declaring it. However, the runtime type indicated by the object variable at Compilation Time can be its own type or its subtype.

Polymorphism
1. The object instance cannot be changed if determined (objective)
2. Only methods defined by the compile-time type can be called.
3. The methods defined in the corresponding type are called according to the runtime type.

The significance of polymorphism: When the commonality of a class of objects needs to be used, the differences in its subclass can be shielded with more than one method.

Note: the attributes of a class are not polymorphism and will only be accessed Based on the type at compile time. Polymorphism occurs only when the subclass covers the parent class method and the subclass object is treated as the parent class type. You must note that the method overload in the subclass is distinguished. For method overloading, the compile-time type is used for corresponding method calls.

Two multiplexing Methods
White box reuse, that is, inheritance reuse. In the parent class, the content that can be accessed by the quilt class can be inherited. In this way, some unnecessary content is inherited, so this method is not very good.
Black box multiplexing, also called combination multiplexing, means that the object of the class to reuse code is used as an attribute in this class, and then the method is delegated to achieve reuse by choice, the method delegate is to call the class method through the class object within the class method.

Note: Use combination reuse instead of inheritance reuse.

Use of Polymorphism

Polymorphism is used for parameters. You can input the parent class type in the method parameter. During running, the corresponding operation is performed in the method according to the actual runtime type.
Polymorphism is used for the return value. It can be the parent type of the actual return value of a method. It is no longer concerned about the actual type of the return value during use.

Polymorphism can make the code more generic to adapt to changes in requirements. That is, the method defined in the parent class can be overwritten by different implementations in the subclass, and the object instance of the subclass of the required function is assigned to the object variable of the parent class.

Related Article

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.