Pages

Multiple Inheritance in java

Its obvious that there are lot of problems related to ambiguity in multiple inheritance. While C++ found a solution to the problem, java decided not to go with multiple inheritance. Instead, they have a different way of doing it. Before implementing multiple inheritance equivalent in Java, you need to be familiar with a few terms.

Inheritance : It is the case when a child class extends a parent class. This is a "is a" relation.

Association: It is the situation where a class may have one or more objects of another class. There is a "has a" relationship between one object to another. The association may be strong when deleting the main objects automatically deletes the other objects as well. While if the other objects still have some value without the main object, such association is called weak association.



Now, to get the effect of multiple inheritance, the class should extend one class, should have another object in it as in association, implement the same interface that the associated object is using and should delegate the interface method calls to the associated object.

Example:

Student(name, age, gender, gpa), staff(name, age, gender, salary), StaffStudent(name, age, gender, gpa, salary)

Given is the scenario. There is a student class which has attribute name, age, gender and there is  a Staff class with attributes name, age, gender, salary. We need to represent it in class diagram. We can see that name, age, gender are common attributes and so, another class named Person could be created out of it.

Person.Java

public class Person {

 String name;
 
 int age;
 
 char gender;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public char getGender() {
  return gender;
 }

 public void setGender(char gender) {
  this.gender = gender;
 }
 
}


Student.java

public class Student extends Person{

 double gpa;

 public double getGpa() {
  return gpa;
 }

 public void setGpa(double gpa) {
  this.gpa = gpa;
 }
 
}

Lets create an interface called IPayable which will contain methods for salary.

public interface IPayable {

 public void setSalary(double salary);
 public double getSalary();
}

Staff.java

public class Staff implements IPayable{

 double salary;

 @Override
 public void setSalary(double salary) {
  this.salary = salary;
 }

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

Now we need to create another class which has the property of both the student and the staff.

In C++ we would inherit both of the class directly. In Java we use extend the one class and have an association with the other class. If you follow proper Design By Interfaces approach, it works pretty well.

public class StudentStaff extends Student implements IPayable{

 Staff staff = new Staff();
 
 @Override
 public void setSalary(double salary) {
  staff.setSalary(salary);
 }

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

}

Now, this class has the property of both the student and staff in java.

No comments:

Post a Comment

If you like to say anything (good/bad), Please do not hesitate...