Personal notes.
The three giants encapsulate inheritance and polymorphism, and inheritance as one of them, its importance is self-evident. The main points of inheritance comparison:
1. Basics of Inheritance: Subclasses use the extends keyword to inherit the non-private properties and methods of the parent class.
2. Benefits of Inheritance: Once a parent class is created, the common characteristics of a series of subclass objects are defined.
3. Subclass calls the parent class method using the Super keyword.
Inheritance of constructors:
Both the parent and child classes have their own constructors, but what is the constructor responsible for loading the subclass object? Is it the responsibility of the parent class or the child class? The answer is: the parent class is responsible for constructing the parent class part, and the child class constructs the subclass part. Let's do an experiment.
classshape{inth; intW; Publicshape () {System.out.println ("Super-shape"); } }classTriangleextendsshape{intLineNum; PublicTriangleintWintHintlinenum) { This. LineNum =LineNum; System.out.println (H+w+linenum); } } Public classExtendstest { Public Static voidMain (string[] args) {triangle T=NewTriangle (5,4,3); }}
Output Result:
Super-shape
12
proves that the constructor method of the parent class is loaded.
Java high-frequency words--inheritance