I was looking simple abstraction example as follows ,
public abstract class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
}
public class Salary extends Employee {
private double salary; //Annual salary
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
}
public class AbstractDemo {
public static void main(String[] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
}
}
Here I have an abstract base class and concrete sub-classes. The abstract class has 3 parameters in constructor and subclass has four, but when I initialize both the abstract and concrete class in the main method, both constructors are passed 4 parameters and using setSalary() method I am able to calculate a salary for both s and e even thought the Employee abstract class only takes 3 parameters.
How is this happening? Could someone please help me with this?