OOP object-oriented has three features
1. Encapsulation
The benefits of encapsulation: play a certain protective role
Encapsulated is the attribute.
How Java is encapsulated;
1. Encapsulate using the private keyword object property. Can only be used in this class.
2. Provide methods for each attribute. Setxx and Getxx
Set get shortcut key: (alt+shift+s alt+shift+r alt+shift+a alt+shift+o)
2. Inheritance
The format of the inheritance;
public class subclass name extends parent class name {
}
* Note: A subclass can have only one parent class. But allow multiple layers of inheritance
* Method Overrides:
If a method with the same name as the parent class appears in the subclass. This method is called method rewriting.
The method name is the same as the method name in the parent class and the parameter list is the same, and the return type is the same.
The access modifier must not be less than the access modifier of the method in the parent class.
The reason for overriding is because the method in the parent class does not meet the subclass requirements.
* When instantiating subclass objects, the order in which the methods are called is constructed. Instantiates the parent class object before instantiating the subclass object.
3. polymorphic
Role: With a variety of identities, improve the scalability of the program.
1. Polymorphism of the method. Method overloading and overriding.
2. Polymorphism of the object.
① upward transformation. The subclass object is converted to the parent class object. Automatically
② downward transformation. The parent class object is converted to the subclass object. (mandatory) If the parent object is to invoke a method of a subclass, the object declaration of the subclass must first be made, or a class conversion exception will occur at run time. classcastexception
* Access modifiers.
1 public . Anyone can access it.
2.protected protection. The same package and sub-classes under different packages can be accessed.
3. The default. can only be accessed under the same package.
4. Private. can only be accessed in this class.
Level order:public>protected> default >private.
* Anonymous Object . The object can only be used once. It becomes rubbish. New class name ();
* Object Array : A container that holds the reference address of an object. Class name [] Array name =new class name [length];
The main method of string args[] is an array of objects. So the parameters in the main method itself are in the object array.
Java Basic Note 7