1

if i have this abstract class :

package com.abc;

public abstract class Player {
    private String name;

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

When I extend a new class from the abstract one, does the new class uses the inherited constructor by default, or must i create a constructor that calls "super" ?

Dror
  • 5,107
  • 3
  • 27
  • 45

3 Answers3

5

By default, it would use the default empty constructor; since you explicitly provide a constructor you do not get the default constructor. Thus, yes you must explicitly call super(String). If you don't want that restriction, then you should add an empty constructor to your Player.

protected Player() { // <-- only visible to sub-classes.
    // ... implicit super();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Nice workaround. Have an upvote. Is this idiomatic Java? – Bathsheba Jun 21 '17 at 10:17
  • @Bathsheba Idiomatic in which sense? It's an abstract class, so it can't be instantiated without sub-classing anyway. But I think yes, also consider package private (e.g. no access modifier)... then you might have a Factory (or Builder) in that package and only it (or something else in that same package) can instantiate the class. Same idea, just limiting it to sub-classes. – Elliott Frisch Jun 21 '17 at 10:22
3

By default Java compiler will add the default constructor and super() in it, if you did not add a constructor explicitly. Because of your Player class has parameterized constructor you need to call the super

must i create a constructor that calls "super" ?

Yes, you need to explicitly add default constructor.

public Player() {}

Or,

You can add super() with parameter in your subclasses. Like below:

public ChildClass(String name) {
    super(name);
}

If you want you can also overload the constructor. read more about constructor overloading and about super().

Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • I am not sure to understand the second part of the answer, OP was mentionning the need to call super from the sub class. So the same problem occurs. – AxelH Jun 21 '17 at 10:24
  • @AxelH Yes thank you, He asked for class that inherit `Player`. I got it. I will update – Blasanka Jun 21 '17 at 10:26
  • @AxelH is it right now. again thanks for the mention. – Blasanka Jun 21 '17 at 10:42
  • Just one detail, the first statement is true only if the is no constructor define in the class. So in the case of the Abstract class above, the compiler will not add that default empty constructor. – AxelH Jun 21 '17 at 10:45
2

Since your base class Player is missing a default constructor (the automatically generated one is effectively deleted if you supply any other constructor), all constructors in derived classes will need to call the constructor from a String explicitly.

You do that by using super with an appropriate value for name passed. That has to be the first statement in any child class constructor.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483