There are three main features of Java object-oriented design: encapsulation, inheritance and polymorphism
a package of
1.1 Concept: It is to hide some of the class's sensitive information in the class section of the class, not directly accessible to the outside world, but can be accessed indirectly through the Getter/setter method.
1.2 Purpose: We can and may, in the method, add their own restrictions to ensure the validity of the data, in a way, the package also improves the robustness of the code, security.
1.3 Implementation steps: All member variables are privatized, format: Private data type variable name, provides access to member variable Getter/setter method.
Ii. inheritance
2.1 Concept: Subclasses accept all public, protected member variables and member methods of the parent class in a way.
2.2 Purpose: Enhance the reusability of code and improve the quality of code.
2.3 Implementation steps: Using extends keyword, identity subclass inherits parent class, format: public class subclass name extends parent class name
2.4 Note: The ⑴ subclass can override the method of the parent class on the basis of inheriting the parent method, and in the concrete execution is the method of executing the subclass;
⑵ subclasses can define their own properties, methods, which is a manifestation of code extensibility;
All classes in ⑶java, if no defined definition inherits from, the default is inherited from Object;
⑷ (special note) in Java, each class can inherit only one class ———— Java's single-inheritance mechanism.
Tri-Polymorphism
3.1 Concept: In the course of operation, the same type behaves differently under different conditions, and the representation of this indeterminate state can be called polymorphism.
3.2 Category: Ⅰ. Design-time polymorphism: Polymorphism of method overloading implementations
⑴ method overloads ————— All methods defined are declared in the "same class";
The implementation of the ⑵ method overload ———— the same method name (different parameters) {}, for example: public void person () {...}
and public void person (String name) {...}
Ⅱ. Run-Time polymorphism: Method "Override" the implementation of polymorphism
The ⑴ method overrides/Overrides ———— defined methods are definitely methods that override the parent class in a subclass, and the method appears in two classes.
The implementation of the ⑵ method overrides ———— subclass inherits the parent class and then re-sorts the implementation of the clothes class method in the subclass.
Example: Parent public class pet{}
Subclass public class cat{} subclass inherit parent Cat extends Pet
Pet cat = new Cat (); The reference to the parent class points to the object of the subclass, which represents the run-time polymorphism.
There are three main features of Java object-oriented design: encapsulation, inheritance and polymorphism