(1) Used to improve code reusability
(2) Encapsulate the same functional module into a method and call the method when needed to achieve the purpose of code reuse
(3) Extract the same class content from multiple classes through inheritance and turn it into a new class, allowing other classes to have a relationship with the current new class to achieve the purpose of code reusability. Note: Java is a single Inheritance is different from c. C can have multiple inheritance.
Keyword extends
*The subclass has the non-private attributes and methods of the parent class.
*Subclasses can have their own properties and methods, that is, subclasses can extend parent classes.
*Subclasses can implement the methods of the parent class in their own way.
*Java inheritance is single inheritance, but multiple inheritance is possible. Single inheritance means that a subclass can only inherit one parent class. Multiple inheritance means that, for example, class B inherits class A, and class C inherits class B, so According to the relationship, class B is the parent class of class C, and class A is the parent class of class B. This is a feature that distinguishes Java inheritance from C inheritance.
*Improved the coupling between classes (disadvantages of inheritance, high coupling will cause the closer the connection between codes, the worse the code independence)
As shown below:
Picture source Han Shunping b station link
It can be seen that the subclass object has all the attributes of the parent class, but if we access Who will name visit?
Note: Applicable to both properties and methods. Default means nothing needs to be written in front of the return type (data type).
The above is the detailed content of What is the essence of object-oriented inheritance in Java?. For more information, please follow other related articles on the PHP Chinese website!