(i) Invocation of construction method under inheritance condition
Take the following source code as an example:
PackageKtdemo;classGrandparent { Publicgrandparent () {System.out.println ("Grandparent Created."); } PublicGrandparent (String string,string str) {System.out.println ("Grandparent created.string:" + String + "+str); }}classParentextendsGrandparent { PublicParent () {Super("called", "this method"); System.out.println ("Parent Created"); //super ("hello.grandparent."); }}classChildextendsParent { PublicChild () {System.out.println ("Child Created"); }} Public classTestinherits { Public Static voidMain (String args[]) {child C=NewChild (); }}
The code that modifies the parent constructor method, preceded by a statement that calls the Super method, can display another constructive method that calls grandparent.
Do not add super statement before run:
Add Super statement before running:
From the view of the instructions can be super call the parent class of the argument and no argument of the construction method
#而且必须是子类构造方法的第一个语句, because the call to construct a method is to call the constructor of the parent class before calling the constructor of the child class.
(b) What is the constructor of a subclass that must call the constructor of the parent class before it is run? Can it turn out, why not in turn?
(1) A constructor is a special function that is used primarily to initialize an object when it is created, that is, to assign an initial value to an object member variable, which is used with the new operator in the statement that creates the object; The main function of the constructor is to define the initialization state at the time the class is created
(2) When you construct an object, you must first call its constructor to initialize its member variables and member methods
(3) A subclass has a member variable and a member method of the parent class, which inherits the various properties of the parent class, and the constructor method is equivalent to instantiating the class, and if the subclass does not call the constructor of the parent class, the various properties inherited from the parent class cannot be properly initialized. It cannot be called in turn because the parent does not know what variables and methods are in the subclass, and the class does not get the initialized parent class property, and the program goes wrong.
(c) method overrides require that the subclass be exactly the same as the method of the parent class, otherwise the method overloads, in the subclass, to invoke the overridden method in the parent class, you can use the Super keyword, the sample code:
Packagedemo;classparent{ Public voidShow () {System.out.println ("This is the Show method in the parent class"); }}classChildextendsparent{ Public voidShow () {Super. Show (); System.out.println ("This is the Show method in the subclass"); }} Public classTest { Public Static voidmain (String [] args) {child C=NewChild (); C.show (); }
Run:
Result Analysis: The Super method is used in the subclass override method class to invoke the overridden method in the parent class, and the super statement is placed in the subclass override method.
Some questions in Java Learning (2)