1.
why the constructor of a subclass must call the parent class before it is run
construction method? Can you turn around? Why can't it be reversed? Because the subclass inherits the parent class, it defaults to the public member methods and public member variables that contain the parent class, and these methods and variables are not repeated in the subclass. If the parent class is not initialized when the subclass is initialized, what happens when the parent class method or variable is called by the subclass? Of course, the exception is thrown! Therefore, the Java virtual opportunity initializes the subclass's parent class by default when the subclass is initialized. And it is a layer of upward progression! 2.
Please
Write your own code to test the following features
:
In a subclass, to invoke the overridden method in the parent class, you can use the
Super
key word.
Packagetest;classplus1{ Public intAddinti) {returnI+1;} }classPlus2extendsplus1{ Public intAddintj) {returnJ+10;} Public intADD1 (intK) {return Super. Add (k);} } Public classfruit{ Public Static voidMain (String args[]) {Plus2 F=NewPlus2 (); System.out.println (F.add (1)); System.out.println (F.ADD1 (1)); }
Course Assignments Five