Understanding of Java encapsulation, inheritance and polymorphism

First, briefly describe the definition of its three characteristics:

Encapsulation: hide the attributes and implementation details of the object, only expose the interface, and control the access level of attribute reading and modification in the program. Combine the abstract data and behavior (or function) to form an organic whole, that is, organically combine the data with the source code of operating data to form a "class" Where both data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming. Users do not need to know the specific implementation details, but only use class members through external interfaces and specific access rights. The basic requirement of encapsulation is to privatize all properties and provide getter and setter methods for each property. If there is a constructor with parameters, you must write a constructor without parameters. During development, you often have to test the classes you have written, so sometimes you can rewrite the toString method, but this is not necessary.

Inheritance: code reuse through inheritance. All classes in Java inherit Java directly or indirectly 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 with private access in the parent class. Subclasses can override the methods of the parent class 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. Reduce inheritance as much as possible in development in order to reduce the coupling degree of the program.

Polymorphism: polymorphism is divided into design time polymorphism and runtime polymorphism. For example, overloading is also called design time polymorphism. For overridden or inherited methods, the Java runtime system determines which method to call according to the type of the instance calling the method, which is called runtime polymorphism. In a word, the typical characteristics of object-oriented design are inheritance, encapsulation and polymorphism. These characteristics are also the key to the popularity of object-oriented design.

encapsulation

The default value of the access permission of the class attribute in Java is not private. To hide the method of the attribute, you can add the private modifier to restrict access only inside the class.

For the private attribute in the class, a pair of methods (getxxx, setXXX ()) should be given to access the private attribute to ensure the operation and security of the private attribute.

Method encapsulation, the disclosure of the disclosure, and the hiding of the hiding.

Java inheritance

Inheritance is to re Abstract many kinds of things with common characteristics into a class.

The extensions keyword should be used for inheritance in Java, and single inheritance is allowed in Java, that is, a class can only have one parent class.

Constructor cannot be inherited.

Overrides in Java methods

When a subclass has a method with the same name and return the same parameter list as the accessible method in the parent class, the method 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 means that there are multiple methods with the same name in the same class, but these methods have different parameters., Therefore, you can determine which method to call at compile time. It is a compile time polymorphism.

2. Method override: subclasses can override the methods of the parent class, so the same methods will have different manifestations in the parent class and subclasses. In the Java language, the reference variable 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, the reference variable in the interface can also point to the instance object of its implementation class.

When a superclass object references a subclass object with a reference variable, the type of the referenced object rather than the type of the reference variable determines whose member method to call, but the called method must be defined in the superclass, that is, the method overridden by the subclass.

Here we 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 meaning of the above sentence, B determines 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". Does show (b obj) exist in class a? It doesn't exist! So this sentence doesn't apply here? So is this sentence wrong? No! In fact, this sentence also implies this sentence: it still has to be recognized according to the priority of the calling method in the inheritance chain. Therefore, it will find show (a obj) in class A. at the same time, because B rewrites the method, it will call the method in class B. otherwise, it will call the method in class A.

The above is the understanding of the three characteristics of Java encapsulation, inheritance and polymorphism introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>