Java: Inherited class constructor is calling Super

2019-02-21 04:12发布

While creating a java program i encountered a problem,

A subclass constructor is throwing an Error by calling the Superclass's method

The code is similar to this :

class Manage
{
    public static void main(String[] args) 
    {
        Manager m1 = new Manager ( 35 );
    }
}

class Employee
{
        int emp_id;
        public Employee(int id)
        {
                this.emp_id = id;
        }
        public int get_id()
        {
                return emp_id;
        }

}
class Manager extends Employee
{
        public Manager(int id )
        {
                this.emp_id = id ;
        }
}

class Engineer extends Employee
{
        public Engineer(int id)
        {
                this.emp_id = id ;
        }
}

And the error is something like this :

$ javac app.java 
app.java:25: cannot find symbol
symbol  : constructor Employee()
location: class Employee
        {
        ^
app.java:33: cannot find symbol
symbol  : constructor Employee()
location: class Employee
        {
        ^
2 errors

Why does this happen ?

4条回答
Lonely孤独者°
2楼-- · 2019-02-21 04:41

In java, a sub class constructor always calls one of its parent class's constructor. This is neccessary for the class to be initialized properly. Even when it is subclassed, the fields and state must be setup and this is how its done in java. If none is explicitly specified, it is calling the default no-arg constructor.

查看更多
祖国的老花朵
3楼-- · 2019-02-21 04:51

Since you have specified a constructor with arguments, Java does not provide with a default constructor without arguments. You should create one yourself, or explicitly call the constructor you have created, by using super(id) as first line in the extended classes constructors.

查看更多
Viruses.
4楼-- · 2019-02-21 04:56

The error is generated since you didn't define a default constructor (no arguments) in Employee

class Employee {

    private int emp_id;

    public Employee() {
    }

    public Employee(int id)  {
            this.emp_id = id;
    }

    public int get_id() {
            return emp_id;
    }

}

but there are a couple of points to consider: you are setting emp_id via the constructor and defined a getter to read it. It seems that the field was meant to be private. Otherwise you can just access directly.

You already have a constructor in Employee setting the ID no need to define the same constructor in the same class. Just use the constructor of the superclass.

class Manager extends Employee {

    public Manager(int id ) {
        super(id);  // calls the superclass constructor
    }

}

In this case you don't need the default constructor.

查看更多
Ridiculous、
5楼-- · 2019-02-21 05:03

The superclass doesn't have a default constructor. So you need to pass the appropriate constructor arguments to the superclass:

super(id);

(Put this as the top line in both the Manager and Engineer constructors.) You should also remove the this.emp_id = id line, in both cases.

In general, if your constructor doesn't start with a super(...) or this(...) statement (and you can only have one of these, not both), then it defaults to using super() (with no arguments).

查看更多
登录 后发表回答