Java object-oriented core skills and java object-oriented skills
1. Encapsulation
Encapsulation is one of the three main characteristics of object-oriented, that is, hiding the state information of a class inside the class and not allowing external programs to access it directly, this class provides methods to perform operations and access to hidden information.
Advantages of encapsulation: hiding the Implementation Details of classes, allowing users to access data only through the methods prescribed by the program; can easily add access control statements to limit unreasonable operations.
Encapsulation steps: Modify the visibility of attributes to restrict access to attributes. Create a pair of value assignment methods and value assignment methods for each attribute, and add an access control statement to the attribute Assignment Method.
Permission controllers used for encapsulation: private (private), public (public), and protected (open to subclass and classes of the same package)
Extension: static Modifier
Static can be used to modify attributes, methods, and code blocks. Static modified variables belong to this class and do not belong to a certain object. Therefore, all objects created by this class share a static variable.
Static attributes and methods are generally called class attributes and class methods. If it is not modified by static, it becomes the instance attributes and instance methods.
Static block: the static block is loaded only once when the class is created until the program ends. It is usually used to initialize the attribute that only assigns a value once from the beginning of the program to the end of the program.
Syntax: static {// code block}
2. Inheritance
Inheritance is one of the three main characteristics of object-oriented, and is one of the important methods to implement code reuse in Java. Java only supports single inheritance, that is, each class can only have one direct parent class.
In Java, all Java classes directly or indirectly inherit the Object class. The Object class is the ancestor of all Java classes. If the extends keyword is not used when defining a class, the class inherits the Object class directly.
In Java, subclasses can inherit the following "property" from the parent class ":
1. inherit the attributes and Methods Modified by public and protected, regardless of whether the subclass and parent class are in the same package.
2. inherit the attributes and Methods Modified by the default permission modifier (friendly), but the subclass and parent class must be in the same package.
3. the constructor of the parent class cannot be inherited.
4. attributes and Methods Modified by private cannot be inherited.
In addition, the subclass can rewrite (or overwrite) the methods inherited from the parent class as required ). Pay attention to the following points during Rewriting:
1. The method to be rewritten and the method to be rewritten must have the same method name.
2. The override method and the override method must have the same parameter list.
3. the return value type of the override method must be the same as the return value type of the method to be overwritten or its subclass.
4. The access permission of the method to be rewritten cannot be reduced.
Super Keyword:
The "super" keyword is often used in the process of using inheritance for programming.
Super indicates the default reference to the direct parent class Object of the current object. In the subclass, you can use the super keyword to access members in the parent class.
When using the super keyword to access a parent class member, pay attention to the following points:
1. The super keyword must appear in the subclass (subclass method and constructor), rather than in other places.
2. Access the members of the parent class, such as the attributes, methods, and constructor of the parent class.
3. Pay attention to the permission restrictions in the parent class during use. super cannot be used to access the private modified members in the parent class.
Example:
1 super. name; // access the name attribute 2 super of the direct parent class. test (); // access the test method 3 super (name) of the direct parent class; // access the corresponding constructor of the direct parent class, which can only appear in the constructor
Precautions for Constructor in inheritance:
1. If the constructor of the parent class is not displayed in the subclass, the system first calls the constructor without parameters in the parent class.
2. When the program is running, the constructor of the parent class is always called first.
Extension: Overload:
The methods in the same class have different implementations. Only the method name and parameter list must be different, and they are irrelevant to the return value type and access modifier.
3. abstract classes and abstract methods
Abstract classes and abstract methods in C # are basically the same as those in Java. abstract classes and abstract methods in C # have been summarized before. Therefore, this is only a simple summary and review.
Abstract classes and abstract methods in Java have the following features:
1. abstract classes and abstract methods are modified by abstract keywords.
2. abstract classes cannot be instantiated. Abstract classes can have either one or more abstract methods or all of them are abstract methods.
3. abstract methods can only be declared and cannot be implemented. The class of the abstract method must be an abstract class. Subclass must override all abstract methods unless the subclass is an abstract class.
Extension: final Keyword:
Final can be used to modify classes, methods, and attributes. It cannot modify constructor methods.
Final-modified classes are called "final classes" and cannot be inherited.
The final modification method cannot be overwritten.
The final modified attribute becomes a constant and cannot be modified elsewhere except during initialization.
4. Polymorphism
The polymorphism in C # is basically the same as that in Java. Because I have previously summarized the polymorphism in C #, I will only make a summary here.
Polymorphism means that the same implementation interface uses different instances to perform different operations.
Note the following rules when converting a subclass to a parent class:
1. Point a reference to a parent class to a subclass object, which is called upward transformation and automatically performs type conversion.
2. The method called by referencing a variable through the parent class is the method of overwriting or inheriting the parent class by the subclass, not the method of the parent class.
3. Variables referenced through the parent class cannot call the method unique to the subclass.
Note the following rules when converting a parent class to a subclass:
1. The conversion of a parent class to a subclass is called a downward transformation and requires a forced conversion.
2. You can use the instanceof keyword to avoid a field during forced conversion.
Instanceof Keyword:
The instanceof keyword is used to determine whether an object belongs to a class or implement an excuse. The result is true or false. It works the same as the is keyword in C.
5. Interfaces
Interfaces and abstract classes are roughly the same. abstract classes focus on code reuse, while interfaces focus on code extension and maintenance. An interface is a standard that can constrain the behavior of a class.
Interface-oriented programming can reduce code coupling and improve code scalability and maintainability.
During system development, the main architecture uses interfaces to form the skeleton of the system. In this way, you can replace the class implementing interfaces to replace the system.
Interface Definition:
1. The interface naming rules and classes are the same.
For example, public interface name {
}
2. constants can be defined in the interface, but variables cannot be defined. All attributes in the interface are automatically modified using public static final. The constant in the interface must be specified at definition.
3. All related methods in the interface are abstract methods. All methods in the interface are automatically modified using public abstract.
4. The interface cannot be instantiated, and the interface cannot have constructor.
5. interfaces are inherited through extends. One interface can inherit multiple interfaces, but the interface cannot inherit classes.
6. The interface implementation class must implement all the methods in the interface; otherwise, it must be defined as an abstract class.
A class can only have one direct parent class, but multiple interfaces can be implemented through implements. However, if the interface is implemented while the parent class is inherited, The extends keyword must be before the implements keyword.
The use of interfaces in C # is roughly the same as that in Java, but the colon ":" instead of implements is used for implementation.