Ben
Ben

Reputation: 25807

java inheritance problem - have to create empty constructor in father class

I program on netbeans ubuntu java standart project (test preparation). when I create AccountStudent.java I get error.

Account.java

public abstract class Account {
 protected double _sum;
 protected String _owner;
 protected static int accountCounter=0;

 public Account(String owner){
     this._sum=0;
     this._owner=owner;
     accountCounter++;
 }
}

AccountStudent.java- error: cannot find symbol: constructor Account()

public class AccountStudent extends Account{

}

Fix for problem- add empty Account constructor:

Account.java

public abstract class Account {
 protected double _sum;
 protected String _owner;
 protected static int accountCounter=0;

 public Account(){

 }

 public Account(String owner){
     this._sum=0;
     this._owner=owner;
     accountCounter++;
 }
}

Why should i create empty constructor Account if already he exist because he inherit Object class?

Thanks

Upvotes: 2

Views: 7749

Answers (4)

MarkHuntDev
MarkHuntDev

Reputation: 311

An object of an extended class contains state variables (fields) that are inherited from the superclass as well as state variables defined locally within the class. To construct an object of the extended class, you must correctly initialize both sets of state variables. The extended class's constructor can deal with its own state but only the superclass knows how to correctly initialize its state such that its contract is honored. The extended class's constructors must delegate construction of the inherited state by either implicitly or explicitly invoking a superclass constructor.

The Java™ Programming Language, Fourth Edition By Ken Arnold, James Gosling, David Holmes

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346270

Why should i create empty constructor Account if already he exist because he inherit Object class?

Constructors are not inherited. If a class has no explicit constructor, hte compiler silently adds a no-argument default constructor which does nothing except call the superclass no-argument constructor. In your case, that fails for AccountStudent because Account does not have a no-argument constructor. Adding it is one way to resolve this. Another would be to add a constructor to AccountStudent that calls the existing constructor of Account, like this:

public class AccountStudent extends Account{
    public AccountStudent(String owner){
        super(owner);
    }
}

Upvotes: 10

Batakj
Batakj

Reputation: 12743

JLS 8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided. If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.

In this case the AccountStudent class does not have any constructor so the compiler adds a default constructor for you and also adds a call to superclass constructor. So your child class effectively looks like:

    class AccountStudent extends Account{
      AccountStudent() {
      super();
     }
    }

Upvotes: 1

Colin Hebert
Colin Hebert

Reputation: 93167

Every class in Java must have a constructor, if you don't define one, the compiler does that for you and create a default constructor (the one with no parameters). If you create yourself a constructor then the compiler doesn't need to create one.

So even if it inherit from Object, that doesn't mean that it'll have a default constructor.

When you instantiate an AccountStudent, you will need to call the parent constructor. By default if you don't specify yourself a parent constructor to call, it will call the default constructor. If you want to explicitly call a parent constructor, you'll do it with super().

There are three way to avoid the error :

Call the parent constructor with a parameter you get from the child constructor :

public class AccountStudent extends Account{
    public AccountStudent(String owner){
        super(String owner);
    }

}

Call the parent constructor with a parameter you create yourself :

public class AccountStudent extends Account{
    public AccountStudent(){
        super("Student");
    }

}

Call the default parent constructor but you need to create one because the compiler will not create one if a non-default constructor already exists. (the solution you gave)

Upvotes: 3

Related Questions