Little
Little

Reputation: 3477

issue with multiple inheritance in Java

I would like to model the following classes in Java:

enter image description here

so I came with the following code:

class Person
{
    private String name;
    private ing age;
    public Person(String name, int age){
        this.name=name;
        this.age=age;
    }
    //set and get methods
}

class Employee
{
    private String nameEmp;
    private double salary;
    public Employee(String nameEmp, double salary){
        this.nameEmp=nameEmp;
        this.salary=salary;
    }
    public double calcSalary(){}   //should this be an abstract method?
}

class Teacher extends Person implements Employee
{
    private String nameTeacher;
    private int ageTeacher;
    private String title;   //professor or lecturer
    public Teacher(String nameTeacher,int ageTeacher, String title){
        super(nameTeacher,ageTeacher);
        this.title=title;
    }
    public double calcSalary(){
        if (title.equals("Professor")) salary=salary*0,30;
        else if (title.equals("Lecturer")) salary=salary*0,10;
    }
}

I would like to model it using interfaces, but I am not quite sure how to do it. Also the calcSalary should be an abstract method in Employee? How this can be implemented with interfaces in Java?

Thanks

Upvotes: 3

Views: 872

Answers (5)

Syed Muhammad Humayun
Syed Muhammad Humayun

Reputation: 459

public interface Person {
    String getName();
    void setName(String name);
    int getAge();
    void setAge(int age);
}

public class PersonImpl implements Person {
    private String name;
    private int age;
    @override public String getName() { return this.name; }
    @override public void setName(String name) { this.name = name; }
    @override public int getAge() { return this.age; }
    @override public void setAge(int age) { this.age = age; }
}

public interface Employee extends Person {
    double getSalary();
    void setSalary(double salary);
    double calcSalary();
}

public abstract class EmployeeImpl extends PersonImpl implements Employee {
    private double salary;
    @override public double getSalary() { return this.salary; }
    @override public void setSalary(double salary) { this.salary = salary; }
}

public class Teacher extends EmployeeImpl
    @override public double calcSalary() { 
        if (title.equals("Professor")) salary=salary*0,30;
        else if (title.equals("Lecturer")) salary=salary*0,10;
    }
}

Upvotes: 0

Eric Jablow
Eric Jablow

Reputation: 7899

Start with Person. You had a typo there:

public class Person
{
    private String name;
    private int age;
    public Person(String name, int age){
        this.name=name;
        this.age=age;
    }
    // set and get methods, equals, hashCode, toString,
    // perhaps an id for database storage.
}

Now, some Persons are Employees; others are students, parents, prospective students, and so on. If any method of Employee has an implementation, it can't be an interface. And in any case, having nameEmp is wrong since it will duplicate the name in Person. Either you create an interface for Employee that gets mixed into the Teacher class, or you inherit from Employee.

Either:

public interface Employee {
    // Currency values should use BigDecimal or BigInteger, not double.
    BigDecimal salary();
    // Taxpayer Identification Number (SSN) in the USA, or the equivalent outside.
    String taxNumber();
}

or:

public class Employee extends Person {

    private BigDecimal salary;
    private String taxNumber;
    public Employee(String name, int age, BigDecimal salary,
                    String taxNumber) {
        super(name, age);
        this.salary = salary;
        this.taxNumber = taxNumber;
        this.title = title;
    }
    // getters, setters, etc.
}

public class Teacher extends Employee {
    private Department department;
    private List<Course> courses = new ArrayList<>();
    // Constructors, getters, setters, etc.
}

You should only have the salary methods in Teacher if they need a wildly-different implementation. You might want to have some sort of pay calaculator class (the Strategy pattern) in the Employee class instead. This way you can handle taxable deductions, insurance, pension plans, overtime where applicable. I didn't understand the multipliers in your original example.

Upvotes: 0

Kuldeep Jain
Kuldeep Jain

Reputation: 8598

You could have this:

public interface Person{
    // Only abstract methods here
}

public interface Employee extends Person {
    // Only abstract methods here specific to Employee
}

public class Teacher implements Employee {
    //Implements the methods 
}

Upvotes: 3

Mateusz
Mateusz

Reputation: 2317

No, you can't do it this way, you should go for Teacher is -> Employee is -> Person. You can't implement anything in interface! Interface can only contain methods that should be implemented by class.

Upvotes: 4

Anonymoose
Anonymoose

Reputation: 5982

Java 8 will allow you to place default implementations in interfaces. Until then, interfaces cannot contain implementations.

Upvotes: 2

Related Questions