Accessing specific child type method in arraylist of type parent without typecasting, creation of child type is only known at runtime

You can implement Employee as abstract and add getSalary() as an abstract method which forces subclasses of Employee to implement that method

public abstract class Employee {
    private String name;

    public Employee(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    
    abstract protected double getSalary();
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top