Home  >  Article  >  Java  >  What is the essence of object-oriented inheritance in Java?

What is the essence of object-oriented inheritance in Java?

WBOY
WBOYforward
2023-04-26 19:10:061149browse

1.What is inheritance?

(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.

What is the essence of object-oriented inheritance in Java?

2. Inherited format

Keyword extends

What is the essence of object-oriented inheritance in Java?

3. Inherited features

*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)

4. A class inherits from its parent class. When we create a subclass object, what happens in memory? ?

As shown below:

What is the essence of object-oriented inheritance in Java?

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?

What is the essence of object-oriented inheritance in Java?

5. Permission modifier

Note: Applicable to both properties and methods. Default means nothing needs to be written in front of the return type (data type).

What is the essence of object-oriented inheritance in Java?

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete