Java inheritance and coverage is basically a Java written test often out of the problem, but also around, I here to the Java inheritance to make a summary
1. Constructor function:
When a subclass inherits a parent class, the constructor of the parent class needs to be invoked when the subclass is constructed, and there are three scenarios
(1), the parent class does not have a constructor or a parameterless constructor, and if a subclass has no constructors or has parameterless constructors, the subclass constructor does not need to explicitly call the parent class's constructor, and the system automatically calls the parent class's constructor before calling the subclass constructor
(2), the parent class has only the parameter constructor, the subclass must show the constructor of the calling parent class in the constructor, otherwise the compilation error
(3), the parent class has both parameterless constructors and parameter constructors, and subclasses can not invoke the constructor of the parent class in the constructor, when the parameterless constructor of the parent class is used
The above three conclusions have been validated by code
2. Method Coverage:
(1) Subclasses override the parent class and must have the same parameter return type, otherwise the compilation cannot pass
(2) The subclass overrides the parent class, and after jdk1.5, the parameter return class can be the parent class method that returns the subclass of the class
(3) The subclass overrides the parent class method, and the method scope modifier can be modified, but only the scope of the method is magnified, and the public cannot be modified to private
(4) A subclass method can access the protected scope member of the parent class and cannot access the default scope member
(5) A static method of a subclass cannot hide a parent class instance method with the same name
(6) Java and C + +, inherited methods have polymorphism
The above 6 conclusions have been validated by code
3. Member Coverage:
(1) When a subclass overrides a member variable of the parent class, the parent method uses the member variable of the parent class, and the subclass method uses the member variable of the subclass
This sounds easy to understand, but many people are easy to mix in the actual use process: especially in polymorphism, call an inherited method that accesses an overridden member m, and whether the method accesses the parent or member m of the subclass. The conclusion is that if the method of the parent class is actually invoked, the member m of the parent class is used, and if the method of the subclass is actually invoked, use the member m of the subclass, and remember that each class uses a member equivalent to a this pointer in front.
The above 1 conclusions have been validated by code
I see the following example on the Forum, understanding above, you will be able to come up with a good answer:
Class Superclass {private int number; superclass () {this.number = 0;} public superclass (int number) {This.numbe R = number; public int GetNumber () {number++; return number;}} Class SubClass1 extends Superclass {public SubClass1 (int number) {super (number)}} class SubClass2 extends superclass {private int number; public SubClass2 (int number) {super (number);}} public class subclass extends superclass {private int number; public subclass (int number) {super (number);} public int G Etnumber () {number++; return number;} public static void Main (string[] args) {Superclass s = new subclass (20); Superclass S1 = new SubClass1 (20); Superclass s2 = new SubClass2 (20); System.out.println (S.getnumber ()); System.out.println (S1.getnumber ()); System.out.println (S2.getnumber ()); Conclusion One: Polymorphism, when a subclass overrides a method of the parent class, using a subclass-overridden method//Conclusion two: When a subclass overrides a member variable of the parent class, the parent class uses the member variable of the parent class, and the subclass method uses the member variable of the subclass.}
To perform the output:
1
21st
21st