2

If I have an abstract class called Employee having a constructor:

public abstract class Employee {
    //instance fields or attributes of employee
    public Employee(String name, String extensionNumber){
       //initializing the variables
    }       

How am I supposed to write the constructor of a subclass named SalariedEmployee having an additional attribute (not in the super class) ?

Nadim Baraky
  • 459
  • 5
  • 18

4 Answers4

7

You just write a constructor which is able to provide the name and extensionNumber values to the superclass constructor, and do whatever else you like.

I would personally make the Employee constructor protected as well, given that it really is only available to subclasses. Note that apart from that aspect, there's really no difference between the use of constructors in abstract classes and in concrete classes.

public abstract class Employee {
    // ...
    protected Employee(String name, String extensionNumber) {
        // ...
    }
}

public class SalariedEmployee extends Employee {
    // ... (probably a field for the salary)

    public SalariedEmployee(String name, String extensionNumber, BigDecimal salary) {
        // Pass information to the superclass constructor to use as normal
        super(name, extensionNumber);
        // Use salary here
    }
}

Note that it's not required that you have parameters matching the parameters of the superclass, so long as you can provide them in the super call. For example:

public class SalariedEmployee extends Employee {
    ...

    public SalariedEmployee(Employee plainEmployee, BigDecimal salary) {
        super(plainEmployee.getName(), plainEmployee.getExtensionNumber());
        // Use salary here
    }
}

(You might also want to consider making extensionNumber an int instead of a String, as it really is likely to be a plain number - whereas full phone numbers are best stored as strings.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

You would write the following:

public SalariedEmployee(String name, int extensionNumber, BigDecimal salary) {
    super(name, extensionNumber);
    this.salary = salary;
}

A constructor can, as it's first line, call another constructor of the same class using this(args) or a constructor of the superclass using super(args). If no such call is made, then the compiler will automatically insert a call to super() - the no argument superclass constructor. The corollary to that is that if no such call is made, and the superclass does not have a no argument constructor, then it is a compiler error.

From JLS §8.8.7:

The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass (§8.8.7.1).

...

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

Community
  • 1
  • 1
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
2

Just create a constructor having the additional parameter[s] and call the super class constructor using super :

public class SalariedEmployee extends Employee {
    //instance fields or attributes of employee
    public SalariedEmployee(String name, String extensionNumber, int salary) {
       super(name,extensionNumber);
       // more logic
    } 
}
Eran
  • 387,369
  • 54
  • 702
  • 768
2

If the subclass SalariedEmployee has an additional field, then you can create a constructor which accepts this parameter and then calls the parent abstract class constructor:

public class SalariedEmployee extends Employee {
    private String param;

    public SalariedEmployee(String name, String extensionNumber, String param) {
        super(name, extensionNumber);
        // do something with 'param', e.g.
        this.param = param;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360