Three features of object-oriented: encapsulation, inheritance, and polymorphism.
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 attributes) The default value of the access permission for a class attribute 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 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.
This isTree-like inheritance relationshipsJava.
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)
PrivateModifier, indicating onlyInternalAccessible.
DefaultModifier. If the modifier is not added to the method, the default value is default, indicating thatPackage in progressAccessible. Parent and Child classes are in the same package, and child classes can inherit the corresponding content of the parent class. (Class can be modified)
Protected(Protection) modifier, indicatingIn the same packageAccessible,Sub-classes of different packages can also access inheritance.
PublicModifier, indicating public, inAnywhereCan be accessed. (Class can be modified)
The modifier's permissions are gradually extended from top to bottom.
Significance of Inheritance
Child classes can develop the features of the parent class based on the parent class. inheritance can reduce the coupling of the system, that is, the relationship between objects is relaxed, replace the relationship between multiple classes of objects with its parent class object.
Note: constructor methods cannot be inherited..
Determining the attributes and methods of the parent class
From the subclass perspective, we need to look at the commonalities between sub-classes. 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, and class methods are the common behavior of these types of objects. Therefore, you should also pay attention to whether this method is required in all child types when the methods are fixed, methods can be overwritten only when different types of behaviors are used.
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 jdk1.4 previously required method overwrite, the return value, parameter table, and method name of the method must be strictly the same, and the method overwrite in jdk1.5, the Return Value of the method covered in the subclass can be the 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: attributes with the same name in the parent and child classes are not called sub-classes to overwrite the attributes of the parent class, and are called properties 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 an instance of the complete subclass object..
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 () method to access the private attributes in the parent class ),Super can shield conflicts between attributes with the same name in parent and child classes..
Note: When writing a class,Make sure to 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 coverage:
Child classes should cover the parent class methods as little as possible. If most methods of the parent class are covered, we should consider whether there should be an inheritance relationship.
Java Polymorphism(The premise is that the subclass overwrites the method 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:Class attributes are non-polymorphism and will only be accessed according to the compile-time type. Polymorphism occurs only when the subclass covers the parent class method and treats the subclass object 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
1. White Box multiplexing, that isInheritance ReuseThe parent class that can be accessed by the quilt class can be inherited, so that some unnecessary content will be inherited, so this method is not very good.
2. Black Box multiplexing, also calledCombination ReuseThat is, the Class Object of the Code to be reused is used as an attribute in the class, and then the method delegate is used 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 as much as possible.
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.