Ryan is a Java developer working in software engineering research. His research mainly focuses on source code analysis and development tools. Ryan has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

Constructors of Sub and Super Classes in Java?

04.26.2013
| 2803 views |
  • submit to reddit

This post summarizes some commonly asked questions from Stackoverflow.com.

1. Why creating an object of the sub class invokes also the constructor of the super class?

class Super {
    String s;
 
    public Super(){
    	System.out.println("Super");
    }
}
 
public class Sub extends Super {
 
    public Sub(){
    	System.out.println("Sub");
    }
 
    public static void main(String[] args){
    	Sub s = new Sub();
    }
}

It prints:

Super
Sub 

When inheriting from another class, super() has to be called first in the constructor. If not, the compiler will insert that call. This is why super constructor is also invoked in the code above.

This doesn’t create two objects, only one Sub object. The reason to have super constructor called is that if super class could have private fields which need to be initialized by its constructor.

After compiler inserts the super constructor, the sub class constructor looks like the following:

public Sub(){
    	Super();
    	System.out.println("Sub");
    }


2. A Common Error Message: Implicit super constructor is undefined for default constructor

This is a compilation error message seen by a lot of Java developers. “Implicit super constructor is undefined for default constructor. Must define an explicit constructor”

Implicit super constructor is undefined for default constructor












This compilation error is caused because the super constructor is undefined. In Java, if a class does not define a constructor, compiler will insert a default one for the class, which is argument-less. If a constructor is defined, e.g. Super(String s), compiler will not insert the default argument-less one. This is the situation for the Super class above.

Since compiler tries to insert super() to the 2 constructors in the Sub class, but the Super’s default constructor is not defined, compiler reports the error message.

To fix this problem, simply add the following Super() constructor to the Super class, OR remove the self-defined Super constructor.

public Super(){
    System.out.println("Super");
}

3. Explicitly call super constructor in sub constructor

The following code is OK:

sub-constructor-with-parameter











The Sub constructor explicitly call the super constructor with parameter. The super constructor is defined, and good to invoke.

4. The Rule

In brief, the rules is: sub class constructor has to invoke super class instructor, either explicitly by programmer or implicitly by compiler. For either way, the invoked super constructor has to be defined.

5. The Interesting Question

Why Java doesn’t provide default constructor, if class has a constructor with parameter(s)?

Some answers: http://stackoverflow.com/q/16046200/127859



 

Published at DZone with permission of its author, Ryan Wang. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)