简体   繁体   中英

Inheriting a constructor in java

When we use inheritance in java can we inherit constructor of the super class by the subclass?

At tutorialspoint it says:

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

But in somewhere the same tutorial said that:

If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass.

So I want to know can a subclass inherit the constructor of the super class?

So I want to know can a subclass inherit the constructor of the super class?

a constructor cannot be inherited.

Moving on, I've managed to find the source which you're relating to at tutorialspoint .

It's important to note that the section under which it's mentioned is clearly titled as Invoking Superclass Constructor and it states:

If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below.

 super(values); 

To put it simply, what this is trying to say is if the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

However, once there is a parameterized constructor within the super class you must invoke it as the first statement within the constructor of the derived class otherwise your program will not compile.

eg

public class Super { ... }
public class Derived extends Super { ... } // good ! compiler puts in a public default constructor for you.

the code below will not compile and you should get an error stating:

there is no default constructor available in Super

public class Super {
    public Super(int id) {...} 
}
public class Derived extends Super {...}

meaning you'll need to invoke the Super class constructor explicitly as the first statement within the constructor of the Derived class, providing the necessary arguments, eg

public Derived(){ super(12);}

You can access super class constructors with super keyword.

You need to define the constructor in the sub class and invoke the super constructor.

public class Animal {

    private String name;

    public Animal(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void play(){
        System.out.println(this.getName() + " is playing ");
    }
}

public class Dog extends Animal{
    public Dog(String name) {
        super(name);
    }
}

public class Test {

    public static void main(String[] args) {
        Dog d = new Dog("Winky");
        d.play();
    }
}

All the super class constructors are inherited by default, you can call these constructors with the sub class constructor.

When you instantiate sub class object, the sub class before sub class constructor expected it will execute the super class constructor and then it will continue to execute the sub class constructor. If you would like to use other super constructor other than default super constructor you can call with super keyword from the sub class constructor and this call should be the first statement in the sub class constructor.

example:

public Subclass(String msg) {
    super(msg); // calling non default super class constructor
    // any other initialization
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM