Home >Java >javaTutorial >There are many things to pay attention to in Java inheritance
Notes
1. Only multi-layer inheritance is allowed in Java, multiple inheritance is not allowed, and Java has single inheritance limitations.
2. In Java, all classes inherit the Object class directly or indirectly by default.
3. If a subclass inherits the parent class, it will have all the attributes and methods of the parent class! However, it should be noted that all (4) non-private (no private) operations belong to explicit inheritance (can directly utilize object operations), while all private operations belong to implicit inheritance (completed indirectly).
In the inheritance relationship, if you want to instantiate a subclass object, the parent class constructor will be called by default to initialize the attributes in the parent class, and then the subclass constructor will be called to initialize the attributes in the subclass. Property initialization, that is: by default, the subclass will find the parameterless constructor in the parent class.
Ctrl H displays the inheritance tree in the class.
Example
//多层继承 class A {} class B extends A {} class C extends B {} //多重继承 class A {} class B {} class C extends A,B {} // 一个子类继承了两个父类
The above is the detailed content of There are many things to pay attention to in Java inheritance. For more information, please follow other related articles on the PHP Chinese website!