2

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?

2
  • 1
    I read your question once again and found that your question is unclear. Commented Dec 12, 2013 at 13:12
  • i am asking about constructor initialization that is mentioned in answers below Commented Dec 12, 2013 at 13:13

2 Answers 2

4

I'm not sure I understand what you're saying about computesalary() (which isn't in your sample code), but I suspect the confusion might be about this line:

Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);

Here, note that even though you're assigning the reference to a variable of type Employee, you're still creating a Salary object. Therefore you still call the Salary derived class' constructor, which has 4 arguments. This line of code is doing exactly the same thing as the line above it, and executing exactly the same code path. The only difference is that the reference to the newly-created object gets stored in a variable with the base class' type.

As Thomas points out, this would call the base class 3-argument constructor:

Employee e = new Employee("John Adams", "Boston, MA", 2);

But this wouldn't be valid code since Employee is abstract.

Sign up to request clarification or add additional context in comments.

5 Comments

yes , i did not paste whole code i was confused about abstract class constructor that i mentioned in my question title .
...the point being that you're not doing = new Employee here (which you couldn't anyway since Employee is abstract), but = new Salary and that latter one takes 4 parameters.
I see. Your main function is not directly calling the abstract class constructor. That can only be called by derived classes' constructors. The type of the variable which the new object is getting assigned to has no effect on how the code runs.
@Thomas then if we have to use subclass construct whats the point to have abstract class in this scenario ?cant we use only salary class to make the whole logic? (i was following a tutorial this code is not by me )
@JavaStudent You could do it all in one class, yes. The benefits of having an (abstract) base class are numerous and well covered elsewhere. It's the heart of object-oriented design.
0

In both cases you are creating an instance of Salary class, thus the constructor that's being called comes from Salary class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.