简体   繁体   中英

how to convert this multiple inheritance to interfaces in Java?

I want to implement the following multiple inheritance case:

(took from http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/ )

Person           Employee
   |_________________|
              |
           Nurse

at the end I want to print the salaries from Employee and Nurse. In C++ it is easy to be done by using multiple inheritance, but I have problems with Java.

I have the following codes:

public class Person{
    protected String name;
    protected int age;
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
}

and the interface:

public interface Employee{
    public double getSalary();
}

and the class Nurse:

public class Nurse extends Person implements Employee{
    private double salary;
    public Nurse(String name, int age, double salary){
        super(name,age);
        this.salary=salary;
    }
    @Override
    public double getSalary(){
        return salary;
    }
}

but I do not have a clue how to make the Employee to print its salary, because it is an interface. I do not want to use other abstract class called Employee. How to fix this?

Thanks

Assuming that all employees are people (unless you are hiring monkeys!!), could you not chain the classes?

public class Person {
    private String name;
    private int age;

    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
}

public class Employee extends Person {
    private double salary;

    public double getSalary(){
        return salary;
    }
}

public class Nurse extends Employee {
}

Java allows multiple inheritance of interfaces only, so in order to implement that you have to define an interface for every class you'd like to combine and compose them in a single class by delegating all methods derived from the partial interfaces to their implementation.

Person interface - as you've defined it

PersonImpl class :

public final class PersonImpl implements Person {
    private final String name;
    private final int age;

    public PersonImpl(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getAge() {
        return age;
    }
}

EmployeeImpl class :

public final class EmployeeImpl implements Employee {
    private final double salary;

    public EmployeeImpl(double salary) {
        this.salary = salary;
    }

    @Override
    public double getSalary() {
        return salary;
    }
}

Nurse class - composed from to other classes and delegates the functionality to them:

public class Nurse implements Employee, Person {

    private final Employee employee;
    private final Person person;

    public Nurse(String name, int age, double salary) {
        person = new PersonImpl(name, age);
        employee = new EmployeeImpl(salary);
    }

    @Override
    public double getSalary() {
        return employee.getSalary();
    }

    @Override
    public String getName() {
        return person.getName();
    }

    @Override
    public int getAge() {
        return person.getAge();
    }
}

It's advisable to define an interface for every class you code in order to able that approach in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM