Java basics: encapsulation inheritance polymorphism

The three basic characteristics of object-oriented are: encapsulation, inheritance, and polymorphism.

We know that encapsulation can hide implementation details and make code modular; inheritance can extend existing code modules (classes); their purpose is: code reuse. And polymorphism is to achieve another purpose-interface reuse! The role of polymorphism is to ensure that a certain attribute of an instance of any class in the "family tree" is used correctly when a class is inherited and derived.

Package:

 Encapsulation is the first step to realize object-oriented programming. Encapsulation is to gather data or functions in individual units (we call them classes). The encapsulated object is usually called an abstract data type. 

 The meaning of
  encapsulation : The meaning of encapsulation is to protect or prevent code (data) from being unintentionally destroyed by us. In object-oriented programming, data is regarded as a central element and is closely integrated with the functions that use it, so as to protect it from being accidentally modified by other functions.

  1. Protect data members and prevent direct access or modification by programs other than the class. They can only be accessed through the provided public interface ==> data encapsulation.
  2. The details of the method are hidden from the user. As long as the interface remains unchanged, the modification of the content will not affect the external caller ==> method encapsulation. 
  3. When the object contains complete attributes and corresponding methods, it is called encapsulation.
  4. The properties of the object cannot be accessed directly from outside the object, but can only be accessed through the method corresponding to the property.
  5. The methods of the object can receive messages outside the object.

Access modifiers for class members:

That is, access control symbols for class methods and member variables. A class as a whole object is invisible, which does not mean that all its fields and methods are also invisible to other parts of the program, and their access modifiers are required to judge.

Authority In-class Same package Different types of buns Different packages are not sub-categories
private × × ×
default × ×
protected ×
public

inherit:

Inheritance mainly realizes the reuse of code and saves development time.

Inheritance can solve the problem of code reuse and make programming closer to human thinking. When multiple classes have the same attributes (variables) and methods, the parent class (such as the Student just now) can be abstracted from these classes, and the parent class (such as the Student just now) can be abstracted from these classes. These same properties and methods are defined in the class. All subclasses do not need to redefine these properties and methods, but only need to declare the inheritance of the parent class through the extend statement:

class subclass extends parent class

In this way, the subclass will automatically have the properties and methods defined by the parent class.

Note on inheritance:

  • The child class inherits at most one parent class
  • Many java classes are subclasses of Object class

Method overloading:

To put it simply: Method overloading is the multiple realization of the same function of the class. Which method is used depends on the parameters given by the caller.

Method overloading-considerations

  • Same method name
  • At least one of the method parameter types, number, and order are different
  • Only the return type is different, it cannot constitute an overload
  • Method modifiers can be different
  • If only the control access character is different, it cannot constitute an overload

Method coverage:

Rewrite the method of the parent class. Method coverage means that the subclass has a method that has the same name, return type, and parameters as a method of the parent class. Then we say that the method of the subclass covers the method of the parent class, such as the Cat class in the previous case. The cry method in the overwrites the cry method of the Animal class.

Precautions for method coverage:

  • The return type, parameters, and method name of the subclass method must be exactly the same as the return type, parameters, and method name of the parent method, otherwise the compilation error will occur.
  • Subclass methods cannot narrow the access rights of parent methods. Assuming that the parent class method is public, but your subclass method is changed to protected, an error will occur

Polymorphism:

1. "One interface, multiple methods"

The so-called polymorphism refers to the multiple states of a reference (type) in different situations. You can also understand it this way: polymorphism refers to calling methods implemented in different subclasses through a pointer to the parent class. The same operation acts on different objects, can have different interpretations, and produce different execution results.

  Three conditions for polymorphism:

  • The existence of inheritance (inheritance is the basis of polymorphism, there is no polymorphism without inheritance).
  • The subclass overrides the parent class's method (call the subclass override method under polymorphism).
  • The parent class reference variable points to the subclass object (type conversion from subclass to parent).

Overload and override are the two main ways to achieve polymorphism.

2. Realize polymorphism:

  • Interface polymorphism.
  • Inherit polymorphism.
  • Polymorphism achieved through abstract classes.
     

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/105282526