Broken Constructor Chains
CIT 594, Spring 2005

When you write a class, Java automatically provides a default constructor for that class, unless you write one yourself.

public class MyClass {
    // my variables
  
    public MyClass() { // added by Java
        super();
    }
  
    // my methods
}
public class MyClass {
    // my variables
 
    public MyClass(int foo) {
        // my constructor code
    }
  
    // NOTHING added by Java
  
    // my methods
}

When you write a constructor, Java automatically inserts a call to the constructor of its superclass.

public class MyClass {
  
    public MyClass() {
        super(); // added by Java
        // my constructor code
    }
  
}

But if you extend a class for which you wrote a constructor, hence that class has no default constructor, then calls to that default constructor will not be legal (because there is no such constructor)..

public class MySuperClass {
    // my variables
  
    public MySuperClass(int foo) {
        // my constructor code
}   // NOTHING added by Java   // my methods }
public class MyClass extends MySuperClass { 
    // my variables
  
    public MyClass() { // added by Java and NOT LEGAL! 
        super();
    }
  
    // my methods
}

Solution: Make an explicit call to your superclass constructor:

public class MyClass extends MySuperClass { 
    // my variables
  
    public MyClass(int foo) { // written by ME!
        super(foo);
    }
  
    // my methods
}