Java basic learning day39 (inheritance)

1. What is inheritance and the benefits of inheritance?

  • Inheritance is one of the three major characteristics of object-oriented, which can create a parent-child relationship between classes
  • The repeated code in multiple subclasses can be extracted into the parent class, and the subclasses can be used directly, reducing code redundancy and improving code reusability

2. Inherited format
public class subclass extends parent class { }

Subclass: also called derived class
Parent class: also called base class or super class

3. Characteristics of subclasses after inheritance

  • Subclasses can obtain the properties and behaviors of the parent class, and subclasses can also directly use the properties and methods of the parent class
  • The subclass can add other functions on the basis of the parent class, and the subclass is more powerful
  • Subclasses can only call non-private members of the parent class, because private (private) can only be used in this class
  • A subclass can have a method with the same name as the parent class, but when creating a subclass object and calling the method with the same name, the method in the subclass is used, and the method in the parent class is overwritten.

4. When to use inheritance?
When the same (common) content exists between classes and the subclass is one of the parent classes (considering reality), you can consider using inheritance to optimize the code, for example: employee: project
insert image description here
insert image description here
manager , Programmer // ✔
Stuff: Programmer, Phone // ✘ Programmers are not Stuff

5. Characteristics of Inheritance

  • Java only supports single inheritance, does not support multiple inheritance, but supports multi-level inheritance

Single inheritance: a subclass can only inherit one parent class
Multiple inheritance is not supported: a subclass cannot inherit multiple parent classes at the same time
Why not support it, as shown in the figure:
insert image description here

Multi-layer inheritance: subclass A inherits parent class B, and parent class B can inherit parent class C.
Direct inheritance is called: direct parent class.
Indirect inheritance is called: indirect parent class. As shown in the figure,
insert image description here
each class directly or indirectly inherits from Object (The largest ancestor class), if class A does not write who it inherits from, it will inherit from the Object class by default (automatically written by the virtual machine), if A inherits from B, it will be pushed up in turn.

Any subclass can use the properties and methods of the direct parent class or indirect parent class, but cannot use the properties and methods of the uncle class, as shown in the figure
insert image description here
insert image description here
insert image description here

6. Design methods for inheriting classes

  • Drawing method, as shown in the figure:
    insert image description here

Guess you like

Origin blog.csdn.net/u011453680/article/details/128900462