Domanda

Say I've made an abstract class called animal, and then I make a subclass called dogs which extends animal. The animal class has a constructor method written inside. If I decide to instantiate a "dog" object, will the animal classes's constructor automatically be called as well?

If someone could clarify, and further expand on this a bit, it'd be greatly appreciated!

È stato utile?

Soluzione

Yes , The constructor of an abstract class can be called. why not? Use super() from subclass to call the super class argument constructor(s). If you dont use super(), then by default the no-arg constructor of super class will be called.

If you call

Dog dog = new Dog();

By default the super class default constructor[no-arg constructor] will be called.

If you call

Dog dog = new Dog("doggie1");

Unless you explicitly call super() inside the one argument constructor of Dog, the default super class constructor will not be called.

Try it..

Edit: If you don't call super() inside the one argument constructor of Dog, then also the default super class constructor will be called.

If you want to explicitly call the argument constructor , say one argument constructor of the super class, you have to explicitly make the call super("value");

Animal() {
        System.out.println("Animal superconstructor");
    }

Edit 02:

Sample program and output

(1)

public class SubClass extends SuperClass {
    SubClass(String str) {
        super(str);
    }

    SubClass() {
    }

    public static void main(String[] args) {
        new SubClass("hello");
    }
}

abstract class SuperClass {
    SuperClass() {
        System.out.println("I am SuperClass()");
    }

    SuperClass(String str) {
        System.out.println("I am SuperClass(String str)");
    }
}

//output: I am SuperClass(String str)

(2)

public class SubClass extends SuperClass {
    SubClass(String str) {
    }

    SubClass() {
    }

    public static void main(String[] args) {
        new SubClass("hello");
    }
}

abstract class SuperClass {
    SuperClass() {
        System.out.println("I am SuperClass()");
    }

    SuperClass(String str) {
        System.out.println("I am SuperClass(String str)");
    }
}

//output: I am SuperClass()

(3)

public class SubClass extends SuperClass {
    SubClass(String str) {
    }

    SubClass() {
    }

    public static void main(String[] args) {
        new SubClass();
    }
}

abstract class SuperClass {
    SuperClass() {
        System.out.println("I am SuperClass()");
    }

    SuperClass(String str) {
        System.out.println("I am SuperClass(String str)");
    }
}

// output: I am SuperClass()

Altri suggerimenti

If Animal has a no-arg constructor, then Animal's subclasses will call it automatically. Otherwise, the compiler will complain. In such a case, you have to call super() explicitly with the appropriate parameters for the Animal constructor you wish to use.

If I remember correctly, the abstract class's constructor (only the default constructor) is called by default only in the instance of the default constructor. To call the other constructors of the inherited class, a super() call needs to be made inside the constructors of the subclass.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top