1

If I have a subclass that doesn't have a constructor defined but in the superclass there is one defined, will the subclass use that constructor when the subclass object is instantiated?

  • No. only the invocation of the constructor of the subclass uses the parent constructor. the class itself doesn't. And, yes, every single constructor call calls the constructor of it's parent class – Stultuske May 14 '19 at 13:20
  • if you do not explicitly create a constructor yourself, it will use the default one which is `Sub() { super(); }`. This obviously only works if the parent class has a no-arg constructor. If not, Java will **not compile** your program. – Zabuzard May 14 '19 at 13:49

3 Answers3

3

Does a subclass use parent constructor if one isn't defined in the subclass?

That depends on what you mean by "use." If you mean, does the default constructor for a child class call the parent constructor, then yes, it does (more below). If you mean, is a default constructor matching whatever parameters the parent constructor has created automatically, then no, not in the general case.

When you don't declare any constructors for a child class, the default constructor is supplied for you. It always looks like this

/*same access modifier as the class*/ Child() {
    super();
}

Base classes have a default as well, which looks the same but just doesn't have super();.

So if the parent class has a no-arguments constructor (explicitly, or via the default) then the child class's default constructor will successfully use it. But if there's a constructor defined in the parent class that requires an argument, then the child class won't compile, because the super() in the default constructor doesn't match a constructor in the parent class.

Compare this, which works:

public class Parent {
    public Parent() { // I could have left this off, since it's the default for a
    }                 // base class; it's here for emphasis
    public static void main(String[] args) {
        new Child();
    }
}

class Child extends Parent {
}

with this (added a String param to the Parent constructor), which fails:

public class Parent {
    public Parent(String s) {
    }
    public static void main(String[] args) {
        new Child();
    }
}

class Child extends Parent {
}

The second one fails with:

class Child extends Parent {
^
  required: String
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

If no constructor is written in a class, actually a default constructor is added, which can be seen in the byte code:

class A {
}

will generate code for:

class A extends Object {
    A() {
        super();
    }
}

Every constructor must call a constructor of the parent class as the first statement. Again here there is an implicit call to super().

class B extends A {
    B() {
        System.out.println();
    }
    B(int n) {
        System.out.println();
    }
}

will generate code for

class B extends A {
    B() {
        super(); // A()
        System.out.println();
    }
    B(int n) {
        super(); // A()
        System.out.println();
    }
}

This means one can get an error, that no (overloaded) constructor is available for the given argument types.

An other point is, that in general the statement super(); serves no purpose.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

Say you have an empty child class that extends the parent:

public class TestChild extends TestParent{

}

And the parent looks like:

public class TestParent {

private String testStr;

public TestParent() {
    this.testStr = "I exist in the child class!";
}

public String getTestStr() {
    return testStr;
}

public void setTestStr(String testStr) {
    this.testStr = testStr;
}
}

And you create an object of the child in the main and print it out with:

    TestChild test = new TestChild();
    System.out.println(test.getTestStr());

The result will print out:

I exist in the child class!

This happens because the child class will automatically call the no-arg constructor of the super class. So you do not explicitly need a constructor in the child class as it will automatically generate to you a default constructor.

Nexevis
  • 4,647
  • 3
  • 13
  • 22