Study Notes
First, inheritance
1. Inheritance is a cornerstone of Java Object-oriented programming technology because it allows classes of hierarchical hierarchy to be created.
2, Java inheritance is a single inheritance , not as many inheritance as the interface, but can be multiple inheritance , single inheritance is a subclass can inherit only one parent class, multiple inheritance is, for example Class A inherits Class B, Class B inherits Class C, so according to the relationship is Class C is the Class B parent class , Class B is the parent class of Class A, which is a feature of Java inheritance that differs from C + + inheritance.
3, Disadvantages: Improve the coupling between the class (inheritance of the shortcomings, high coupling will cause the link between the code).
4, inheritance can use extends and implements these two keywords to implement inheritance, and all classes are inherited from Java.lang.Object, when a class does not inherit the two keywords, the default inherits Object (this class in Java.lang in the package, so no importis required) ancestor classes.
5,Super keyword: We can use the Super keyword to achieve access to the parent class members, to refer to the parent class of the current object.
This keyword: point to your own reference.
6, the final keyword declaration class can be defined as a class cannot inherit, that is, the final class, or for the decoration method, the method can not be overridden by the quilt class;
7. Constructors (constructors)
A subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters , and the constructor of the parent class must be explicitly called through the Super keyword in the constructor of the child class with the appropriate argument list.
If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass, or if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor . (The code understands the following)
//Parent Class Public classParentClass {Private intN; ParentClass () {System.out.println ("Parent class constructor: ParentClass ()"); } parentclass (intN) {System.out.println ("Parent class constructor: ParentClass (int n)" +N); This. N =N; } }//sub-class Public classChildClassextendsParentClass {Private intN; ChildClass () {Super(300); System.out.println ("Subclass Constructor: ChildClass ()"); } PublicChildClass (intN) {System.out.println ("Subclass Constructor: ChildClass (int n)" +N); This. N =N; }}//Test Class Public classMaintest { Public Static voidMain (string[] args) {childclass cc=new ChildClass ();ChildClass cc2=NewChildClass (200); }}
The output is:
Parent class constructor: ParentClass (int n) 300
Subclass Constructor: ChildClass ()
Parent class constructor: ParentClass ()// If the Super keyword is not used, the system automatically calls the parent class's parameterless constructor .
Subclass constructor: ChildClass (int n) 200
Java Object-Oriented foundation