Home  >  Article  >  Java  >  Understanding the three major features of Java: encapsulation, inheritance, and polymorphism

Understanding the three major features of Java: encapsulation, inheritance, and polymorphism

高洛峰
高洛峰Original
2017-01-19 14:08:491788browse

First of all, let’s briefly talk about the definition of its three major features:

Encapsulation: hiding the properties and implementation details of the object, only exposing the interface to the outside world, and controlling the reading and modification of properties in the program. access level. Combining the abstracted data and behaviors (or functions) to form an organic whole, that is, organically combining the data with the source code that operates the data to form a "class", in which data and functions are both members of the class. The purpose of encapsulation is to enhance security and simplify programming. Users do not need to understand the specific implementation details, but only need to use the members of the class through the external interface and a specific access permission. The basic requirements for encapsulation are: 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, it is often necessary to test the classes that have been written, so sometimes the toString method is overridden, but this is not necessary.

Inheritance: Code reuse is achieved 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 with private access rights in the parent class. Subclasses can override methods of the parent class and name member variables with the same name as the parent class. But Java does not support multiple inheritance, which is the ability of a class to derive from multiple superclasses. Try to reduce inheritance relationships as much as possible during development to reduce the coupling of the program.

Polymorphism: Polymorphism is divided into design-time polymorphism and run-time polymorphism. For example, overloading is also called design-time polymorphism, and for overridden or inherited methods, JAVA runtime The system determines which method to call based on the type of instance on which the method is called, which is called runtime polymorphism. All in all, the typical characteristics of object-oriented design are inheritance, encapsulation and polymorphism. These characteristics are also the key to why object-oriented is so popular.

Encapsulation

The default value of access permissions for class attributes in Java is not private. If you want to hide the method of this attribute, you can add the private modifier to restrict it to only within the class. access.

For the private attributes in the class, a pair of methods (getXXX, setXXX()) should be provided to access the private attributes to ensure the operation and security of the private attributes.

Method encapsulation, what should be made public, what should be hidden should be hidden.

Inheritance of java

Inheritance is the abstraction of multiple types of things with common characteristics into one class.

Inheritance in java must use the extends keyword, and java allows single inheritance, that is, a class can only have one parent class.

Constructor methods cannot be inherited.

Override in java method

When a subclass has a method with the same name and the same parameter list that is accessible 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 used in the constructor. first sentence.

Polymorphism in java

There are two polymorphic mechanisms: compile-time polymorphism and run-time polymorphism

1. Overloading of methods: Overloading is It means that there are 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. It is a kind of compile-time polymorphism.

2. Method coverage: Subclasses can override the methods of the parent class, so the same method will have different expressions in the parent class and in the subclass. In the Java language, a reference variable of a base class can point not only to an instance object of the base class, but also to an instance object of a subclass. Similarly, a reference variable in an interface can also point to an 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 A
2--A and A
3--A and D
4--B and A
5--B and A
6--A and D
7--B and B
8--B and B
9--A and D

When a superclass object reference variable refers to a subclass object, the type of the referenced object rather than the type of the reference variable determines whose member method is called, but the called method must be in the superclass Defined, that is to say, methods overridden by subclasses.

Let’s use an example to illustrate the meaning of this sentence: a2.show(b);

Here a2 is a reference variable, which is of type A, and it refers to It is a B object, so according to the above sentence, it means that B decides 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 results? Here we ignore the following sentence "But the method called here must be defined in the super class", then 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 needs to be confirmed according to the priority of the calling method in the inheritance chain. That's why it will find show(A obj) in class A. At the same time, because B has rewritten this 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 major characteristics of Java encapsulation, inheritance, and polymorphism introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will promptly Reply to everyone. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to understanding the three major characteristics of Java encapsulation, inheritance, and polymorphism, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn