+7  A: 

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);
    }
}
Michael Borgwardt
That error should be reworded to where it makes sense
TheLQ
@Micheal: there is typo mistake. public Account(String owner){super(owner);} should be public AccountStudent(String owner){super(owner);}.
Shashi Bhushan
@Shashi: thanks, corrected
Michael Borgwardt
+3  A: 

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)

Colin Hebert
+1  A: 

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();
     }
    }
Shashi Bhushan