java encapsulation inheritance polymorphism

Encapsulation, a simple understanding is to put a series of data in a class, if we describe a person (assuming 'person' as a class), we can use height (hight), weight (weight), etc. to describe, if not encapsulated , we need 3 variables to describe it. In object-oriented, you can encapsulate these data with a Person class, Person has 3 member variables, namely height and weight. When used, whenever an object of such a class is generated, it has these three properties.

Inheritance, if class B inherits class A. Then class B will have all the methods of class A, and can also extend its own unique methods and properties. Also use people as an example. 'Person' is the parent class, so 'Men' and 'Women' both inherit from 'Person'. That is to say, both 'men' and 'women' have two attributes of 'height' and 'weight', and at the same time, they can have their own unique attributes. For example 'man' can have a 'wife' attribute to indicate who his wife is. , while a woman can have the 'husband' attribute.

Polymorphism, in java, the same method can have many different manifestations, specifically, it is divided into overloading and rewriting. Overloading is a method with the same method name but a different parameter list. For example:
public String talk(String content); //The parameter is String,
public String talk(String content, int number) //The parameter is 2, String and int
overloading means that the subclass inherits the parent class while renewing the Implements a method of the parent class. The overloaded method names and parameters must be exactly the same.
For example, the parent class
public String talk(String content) {
System.out.print(content);
}
The subclass inherits this class, so it also has the talk method, which redefines the implementation of this method
public String talk(String content) {
System.out.println("Hi" + content);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326836830&siteId=291194637