First, let’s briefly talk about the definition of its three major features:
Encapsulation: hides the properties and implementation details of the object, only exposes the interface to the outside world, and controls the access level of read and modify properties in the program. Combining the abstracted data and behaviors (or functions) to form an organic whole, that is, organically combining data with the source code of operating data to form a "class", where data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming. Users do not have to understand the specific implementation details, but simply use the class members through an external interface, a specific access permission. The basic requirements for encapsulation are: privatize all attributes, provide getter and setter methods for each attribute. If there is a constructor with parameters, then you must write a constructor without parameters. During development, you often have to test the already written classes, so sometimes you can rewrite the toString method, but this is not necessary.
Inheritance: Enable code reuse through inheritance. All classes in Java are obtained by directly or indirectly inheriting the java.lang.Object class. The inherited class is called a subclass, and the inherited class is called a parent class. Subclasses cannot inherit member variables and methods in the parent class whose access permission is private. A subclass can override the parent class's methods and name member variables with the same name as the parent class. However, Java does not support multiple inheritance, that is, the ability of a class to derive from multiple superclasses. In development, the inheritance relationship is minimized, and this is done to reduce the coupling degree of the program.
Polymorphism: Polymorphism is divided into design-time polymorphism and run-time polymorphism. For example, overloading is also called design-time polymorphism. For overriding or inheriting methods, the JAVA runtime system decides which method to call based on the type of instances that call the method, which is called run-time polymorphism. In short, the typical characteristics of object-oriented design are inheritance, encapsulation and polymorphism, which are also the key to the popularity of object-oriented.
Package
The default value of access rights for a class attribute in Java is not private. If you want to hide the method of this attribute, you can add a private modifier to restrict access to the class only.
For private attributes in a class, a pair of methods (getXXX, setXXX()) should be given to access private attributes to ensure operation and security of private attributes.
encapsulation of the method, the disclosure of the disclosure, the hidden hidden.
Java inheritance
Inheritance is to abstract multiple types of things with common characteristics into one class.
Inheritance in java must use the extends keyword, and the java middle finger allows single inheritance, that is, a class can only have one parent class.
The constructor cannot be inherited .
Override in java method
When there are methods in the subclass that return the same parameter list as the same name that can be accessed in the parent class, the methods inherited from the parent class will be overwritten.
super() keyword
super() means that when the constructor of the subclass calls the constructor of the parent class, super() can only be in the first sentence of the constructor.
Polymorphism in java
There are two polymorphic mechanisms: compile-time polymorphism and run-time polymorphism
1. Method overloading: Overloading refers to multiple methods with the same name in the same class, but these methods have different parameters. , so you can determine which method to call at compile time, which is a compile-time polymorphism.
2. Method override: Subclasses can override methods of parent class, so the same method will have different manifestations in parent class and subclasses. In the Java language, reference variables of the base class can point not only to the instance object of the base class, but also to the instance object of the subclass. Similarly, reference variables in the interface can also point to the instance object of its implementation class.
public class A {public String show(D obj) {return ("A and D");} public String show(A obj) {return ("A and A");} }public class B extends A{public String show(B obj){return ("B and B");} public String show(A obj){return ("B and A");} }public class C extends B{}public class D extends B{}public class Test {public static void main(String[] args) {A a1 = new A();A a2 = new B();B b = new B();C c = new C();D d = new D(); System.out.println("1--" + a1.show(b));System.out.println("2--" + a1.show(c));System.out.println("3--" + a1.show(d));System.out.println("4--" + a2.show(b));System.out.println("5--" + a2.show(c));System.out.println("6--" + a2.show(d));System.out.println("7--" + b.show(b));System.out.println("8--" + b.show(c));System.out.println("9--" + b.show(d)); }}1--A and A2--A and A3--A and D4-B and A5--B and A6-A and D7-B and B8-B and B9-A and D When a superclass object refers to a variable to refer to a subclass object, the type of the referenced object rather than the type of the referenced variable determines whose member method is called, but the called method must be defined in the superclass, that is, the method covered by the subclass.
Let’s use an example to illustrate the meaning of this sentence: a2.show(b);
Here a2 is a reference variable, of type A, which refers to the B object. Therefore, according to the above sentence, it means that there is B to decide whose method to call, so a2.show(b) should call show(B obj) in B, and the result should be "B and B", but why is it different from the previous running result? Here we ignore the following sentence "But the method called here must be defined in the superclass", so does show(B obj) exist in Class A? It doesn't exist at all! So this sentence does not apply here? So is this sentence wrong? No! In fact, this sentence also implies this sentence: it still needs to be confirmed according to the priority of calling methods in the inheritance chain. That's why it finds show(A obj) in class A. At the same time, since B overrides the method, it calls the methods in class B, otherwise the methods in class A will be called.
The above is the understanding of the three major features of Java encapsulation, inheritance, and polymorphism introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!