Implicit super constructor classes.Mover() is undefined. Must explicitly invoke another constructor

Not sure what this message means, can I get some help?

Hi @BeshooD ,

Quite less Information provided in your post, but the message means you have s.th. like this

public class BaseClass { 
    public BaseClass(String aParam) { 
// ...
    }
} 


public class SubClass extends BaseClass {
    public SubClass(String aParam, int anotherParam)  {
// ... if no explicit super is called it tries to call implicit super();
    } 
}

as you see, the BaseClass is not providing a default constructor (without patameters), so the SubClass has to call the constructor of the BaseClass (with parameters)

public class BaseClass { 
    public BaseClass(String aParam) { 
// ...
    }
} 

public class SubClass extends BaseClass {
    public SubClass(String aParam, int anotherParam)  {
        super(aParam);
    } 
}

See super for more information…

Cheers
— mnse

1 Like

Add an explicit default constructor to your base class like this

1 Like

Hi,

I think this isn’t always a solution, in case if the aParam is mandatory for the base class to work properly …

Cheers
— mnse

Your code is correct because you are calling super(aParam); so will it will look for a constructor BaseClass(String). I should have studied your code more closely before replying. :wink:

If the SubClass constructor does not call a specific super class constructor the the BaseClass must have an explicit default constructor. This is shown in the sketch below. If you remove the default base constructor then it won’t work and you get the error reported.


void setup() {
  BaseClass obj = new SubClass("Jenifer", 21);
  println(obj);
}

public class BaseClass { 
  String name;
  int age;

  public BaseClass() { 
    // explicit default constructor
  }

  public BaseClass(String aParam) { 
    // ...
  }
} 

public class SubClass extends BaseClass {

  public SubClass(String aParam, int anotherParam) {
    name = aParam;
    age = anotherParam;
  } 

  public String toString() {
    return name + " aged " + age + " years";
  }
}

1 Like

Due to lack of further information from the question I assumed BeshooD made a base class named Mover with a construcor with parameter(s) and not calling them accordingly in the sub class. So I made my example depending on that presumption.
For the rest I’ve re-linked the java tutorial about super, which says …

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

:wink:

Cheers
— mnse

1 Like

The whole thing can be quite complicated but this error can easily be avoided by

  1. Include a default constructor (no parameters) in the base class even if you don’t think you need it.
  2. If a child class constructor calls a base class constructor with super(parameter_list) then there must be a base class constructor with matching parameter list
  3. If a child class constructor calls the default base class constructor with super() then the base class must have an explicit default constructor

Hi @quark ,

In principle I don’t want keep this thread unnecessarily open. But this isn’t true …

Simplest example would be the most of the contributed libraries, which have as mandatory parameter the parent PApplet to get proberly initialized. Imagine they make a default constructor without attributes, you can successfully run the sketch but run into runtime error, as the object can be created but can not be used as not initialized. Sure, you can make a default constructor and throw an exception, but imho that would be quite silly …
So, it would be better to have no default constructor that it would run into build error and it is clear to see that it can’t be used that way! eom.

Cheers
— mnse

PS: Ok! Just one more sentence. There are circumstances which makes it mandatory to have a default constructor… but not always :slight_smile:

1 Like

The default constructor would be used to set default values for the class so in my example we could use either

public class BaseClass { 
  String name =  "default name";
  int age = 0;

  public BaseClass() { 
    // explicit default constructor does nothing
  }

} 

or

public class BaseClass { 
  String name =  "default name";
  int age = 0;

  public BaseClass() { 
    // explicit default constructor initialises attributes
    name =  "default name";
    age = 0;
  }

} 

Every class definition should be autonomous which means they should initialise their own attributes. Sub classes should also initialise their own attributes (if any) and those of the parent class either directly or better still by calling a super constructor.

So I didn’t lie :frowning_face: I just didn’t tell everything. Inheritance is a major topic in OO programming and would have its own chapter(s) in a text book. Can’t possibly cover every aspect here.

1 Like

Well! Let’s stop here, because I agree that it would hardly be possible to cover the whole OO topic by this thread.

Nevertheless, a nice conversation… ​:grin:

Cheers
— mnse

As @mnse mentioned, the most important class PApplet doesn’t have any constructor definition!

All “.pde” files are concatenated as 1 “.java” file and wrapped up as a PApplet subclass.

There’s also no need to call super() either when we create our own PApplet subclass for multi-window sketches.

1 Like

Post deleted because it’s too early in the morning and I should have read the post more carefully. :grin: