Joke Collection Website - Blessing messages - Why does JAVA use encapsulation? What is the function of encapsulation? !

Why does JAVA use encapsulation? What is the function of encapsulation? !

Encapsulation is simply to put a series of data in a class. If we describe a person (assuming that' person' is a class), we can describe it by height, weight and so on. If it is not encapsulated, three variables are needed to describe it. In object-oriented, you can use the Person class to encapsulate this data. People have three member variables, namely height and weight. When used, every time an object of this class is generated, it has these three attributes.

Inheritance, if Class B inherits Class A, then Class B will have all the methods of Class A and can extend its own unique methods and properties. Take people as an example. People are paternal, so both men and women are inherited from people. In other words, both men and women have the attributes of height and weight, and they can also have their own unique attributes. For example, a "man" can have a "wife" attribute to indicate who his wife is. And women can have the attribute of' husband'.

Polymorphism, in java, the same method can have many different manifestations, specifically, it can be divided into overloading and rewriting. Overloading means that a method has the same method name but different parameter lists. For example:

Common string dialogue (string content); //The parameter is a string.

Public String talk (String content, int number)// parameters are 2, string and int.

Overloading means that subclasses inherit from the parent class and re-implement the methods of the parent class. The overloaded method name and parameters must be exactly the same.

Such as the parent class

Common String Dialog (String Content) (

System.out.print (content);

}

Subclasses inherit this class, so there is also a method talk, which redefines the implementation of this method.

Common String Dialog (String Content) (

system . out . println(" Hi "+content);

}