Java Error: Implicit Super Constructor Is Undefined for Default Constructor

Java error: Implicit super constructor is undefined for default constructor

You get this error because a class which has no constructor has a default constructor, which is argument-less and is equivalent to the following code:

public ACSubClass() {
super();
}

However since your BaseClass declares a constructor (and therefore doesn't have the default, no-arg constructor that the compiler would otherwise provide) this is illegal - a class that extends BaseClass can't call super(); because there is not a no-argument constructor in BaseClass.

This is probably a little counter-intuitive because you might think that a subclass automatically has any constructor that the base class has.

The simplest way around this is for the base class to not declare a constructor (and thus have the default, no-arg constructor) or have a declared no-arg constructor (either by itself or alongside any other constructors). But often this approach can't be applied - because you need whatever arguments are being passed into the constructor to construct a legit instance of the class.

Java error: Implicit super constructor is undefined. Must explicitly invoke another constructor

Since BaseClass has a non default constructor, it doesn't have the automatically generated parameterless default contstructor.

Therefore your sub-class can't rely on the default constructor (since it won't be able to call the non-existing default constructor of the base class), so your sub-class must have an explicit constructor that calls the constructor of the base class.

Either a constructor with the same parameters :

public SubClass(AuthDetails auth, String ID) {
super(auth,ID);
...
}

Or a constructor without parameters that gives default values for the base-class's constructor :

public SubClass() {
super(null,"something");
...
}

implicit super constructor Person() is undefined. Must explicitly invoke another constructor?

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.

Reference: http://docs.oracle.com/javase/tutorial/java/IandI/super.html :
(See under section 'SubClass Constructors')

So whenever dealing with parameterized constructors make a super(parameter1, parameter2 ..) call to the parent constructor.
Also this super() call should be the FIRST line in your constructor block.

Java. Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor

Any constructor for any class as you know creates an object. So, the constructor should contain proper initialization code for its class. But if you have some class which extends another one (lets call it "parent") then constructor for the class cannot contain all the code needed for the initialization by definition (for example, you cannot define private fields of the parent). That's why constructor of the class has to call constructor of its parent. If you do not call it explicitly then the default parent constructor is called (which is without any parameter).

So, in your case, you can either implement default constructor in parent or directly call any constructor in the class.

Struggling with default constructors, implicit super constructor undefined

The problem is that a sub class's constructor must first call to the Parent's constructor. If the parent has a default constructor (a constructor with no args) then it is called. In your case you have a constructor with args, so there is no default constructor and you must call the constructor explicitly:

public class Date extends Clubs {
public Date(String day) {
super(null); // You can either pass null or an actual object
/// Rest of the code
}

Another option would be to create a default Parcel instance and pass it instead of passing null. You can also create a constructor that get a Parcel instance:

public class Date extends Clubs {
public Date(Parcel p) {
super(p);
/// Rest of the code
}

Implicit super constructor BaseTestMethod() is undefined for default constructor. Must define an explicit constructor How do i fix this error?

In BaseTestMethod class, add the constructor

public BaseTestMethod(){
.....

}

Or
modify your flockSignIn constructor:

FlockSignIn(String browserType){
super (param defined in BaseTestMethod's constructor);
..
}

Implicit super constructor is undefined with Java Generics

Change your subclass cosntructor to:

public class NewClass<T> extends BaseClass<T> {
public NewClass(T value){
super(value);
}
}

If you don't add super(value);, then compiler will automatically add a super();, which will chain to a 0-arg constructor of super class. Basically, your original subclass constructor is compiled to:

public NewClass(T value){
super();
}

Now you can see that, it tries to call 0-arg super class constructor, which the compiler cannot find. Why? Since in super class, you have provided a 1-arg constructor, compiler won't add any default constructor there. And hence that error.

You can also avoid this problem by giving an explicit 0-arg constructor in your super class, in which case, your original sub class code will work fine.



Related Topics



Leave a reply



Submit