简体   繁体   中英

constructor of subclass in Java

When compiling this program, I get error-

 class Person {
    Person(int a) { }
 }
 class Employee extends Person {
    Employee(int b) { }
 }
 public class A1{
    public static void main(String[] args){ }
 }

Error- Cannot find Constructor Person(). Why defining Person() is necessary?

When creating an Employee you're creating a Person at the same time. To make sure the Person is properly constructed, the compiler adds an implicit call to super() in the Employee constructor:

 class Employee extends Person {
     Employee(int id) {
         super();          // implicitly added by the compiler.
     }
 }

Since Person does not have a no-argument constructor this fails.

You solve it by either

  • adding an explicit call to super, like this:

     class Employee extends Person { Employee(int id) { super(id); } }
  • or by adding a no-arg constructor to Person :

     class Person { Person() { } Person(int a) { } }

Usually a no-arg constructor is also implicitly added by the compiler. As Binyamin Sharet points out in the comments however, this is only the case if no constructor is specified at all. In your case, you have specified a Person constructor, thus no implicit constructor is created.

The constructor for Employee doesn't know how to construct the super-class, Person . Without you explicitly telling it, it will default to trying the no-args constructor of the super-class, which doesn't exist. Hence the error.

The fix is:

class Employee extends person {
    public Employee(int id) {
        super(id);
    }
}

Java actually views this code as:

class Person {
  Person(int nm) { }
 }
 class Employee extends Person {
    Employee(int id) {
        super();
    }
 }
 public class EmployeeTest1 {
    public static void main(String[] args){ }
 }

Since there is no Person() constructor this fails. Instead try:

class Person {
  Person(int nm) { }
 }
 class Employee extends Person {
    Employee(int id) {
        super(id);
    }
 }
 public class EmployeeTest1 {
    public static void main(String[] args){ }
 }

Java provides you with a default constructor which takes no parameters. The constructor also has no body, so it is something like so: public Person() {} . The moment you define you own constructor, your constructor(s) take place of the default one, so in your case, Person(int nm){} takes place of Person() { } . Your call is trying to implicitly call Person() { } and since this constructor no longer exists, the code is failing. Take a look at this previous SO question for more information.

Above answer is correct, some addition: you can straight call to super(int n) to avoid implicitly adding super() here:

class Employee extends Person {
    Employee(int id) { super(int id);}
}
  1. If you write no constructor, compiler implicitly add default (without parameters) to your class.
  2. If you write any constructor - compiler will not add default constructor.

PS:Sorry for my writing. English is not my native language.

This is not 100% related your problem. But this is also related to java constructors. Let's say your Person class has no default constructor and the constructor parameters are not primitive data type. They are objects.

But If you want to create a subclass with a default constructor, here is the solution. And keep it mind you are not allowed to change the super class.

I created a another class called contact.

class Contact{
    private String name;
    private int number;

    public Contact(String name, int number) {
        this.name = name;
        this.number = number;
    }
}

Then here is the code.

//Super Class   -  "Can't change the code"
class Person {
    Person(Contact contact) { }
}

//Sub Class
class Employee extends Person {
    private static Contact c;

    static {
        c = new Contact("Anna",10);
    }

    Employee() {
        super(c);
    }
}

You have to static variable of Contact class to keep a object instance for passing to super class constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM