java neutron class inherit the parent class constructor you inherit?

The answer is: java neutron class will not inherit the parent class constructor, but you can call

 

The parent class Animal:

package encryption;
 
public class Animal {
 
    private String name = "animal";
 
    public Animal() {
        System.out.println(".....this is Animal.");
    }
 
    public Animal(String name) {
        this.name = name;
    }
 
    public void printName() {
 
        System.out.println(this.getClass().getName() + ":" + this.name);
    }
 
}
子类 Dog:

package encryption;
 
public class Dog extends Animal {
 
}

主类 Animal_Test:
package encryption;
 
public class Animal_Test {
 
    public static void main(String[] args) {
 
        Animal t = new Animal();
 
        Dog t1 = new Dog();
 
    }
 
}

The first run results:
..... then the this constructor printing is Animal.
..... then the this constructor printing is Animal.

Modify subclass, the subclass to add an explicit reference without structure:
Package Encryption;
 
public class Dog the extends Animal {
 
    // Dog () constructor with no arguments
    public Dog () {
 
    }
}


Second run results:
At this time ..... this construction method for printing is Animal.
..... this constructor then printing is Animal.


Subclass modified again, without reference to the class constructor add a display to call the parent class constructor with no arguments Super ()
Package Encryption;
 
public class Dog the extends Animal {
 
    // Dog () constructor with no arguments
    public Dog () {
       Super () ; // explicitly call parent class constructor with no arguments
    }
}


Third result:
At this time ..... this construction method for printing is Animal.
..... this constructor then printing is Animal.


Summary:
As can be seen from the results of the run, the results are consistent three runs can draw the following conclusions:

1, results of the first and second comparison results, indicating when the subclass does not explicitly write the constructor with no arguments, the system default is to add a sub-class of the constructor with no arguments;

2, results of the second and third comparison results, indicating subclass is implicit call super subclass constructor with no arguments in (), i.e., an implicit call the parent class's constructor with no arguments;

3, the results of a comprehensive three times concluded that subclass constructors not inherited parent class but can implicitly call the parent class's constructor with no arguments; calling instead of inheriting;
refer to the original: https: //blog.csdn. net / u014143369 / article / details / 53191209

 

The parent class constructor can not be inherited by subclasses

Constructor is not inherited, it can not be rewritten, but can be overloaded.

Cause: The constructor is a way to produce the object, if you can be inherited, then the object can be copied. Subclass can generate parent objects through inheritance constructor, so there will be sub-class reference point to the parent class object, java does not support the transition downward, only upward transition.

Because subclass inherits the parent class when the first run parent class constructor; specifically, is when you run the parent class constructor will be the first "call" parent class is called the "auto-run" inheritance is extended

Subclass completely unnecessary extension of the parent class constructor, the constructor anyway, because every time the tone of the class will "auto-run" of its parent class, if you really need a special form of sub-class constructor, modify or re-direct subclass upload your own constructor enough.

 

"Calling" a class of "inheritance" and "combination (Plainly a new class)" in two ways, when you "call" a class will "auto-run" its "constructors."

So either parent has a default no-argument constructor, so Java will automatically call the constructor with no arguments. If the parent class has no constructor with no arguments, then you would have in your own class's constructor by super () way to call the constructor of the parent class.

 

Super keyword call the object of the parent class
 

Guess you like

Origin blog.csdn.net/weixin_44018338/article/details/91456354