Multiple Inheritance Using Interface in Java
Multiple Inheritance Using Interface in Java
Let’s discuss the first scenario when interfaces haven’t any common method. Here we will create
two interfaces and one concrete class that implements them and provide the implementation to
the abstract method.
Let’s say we have two interfaces CollegeData and HostelData. Both classes declared some
method, but they haven’t any method. Here we have two methods in each class that inherited by
concrete class.
StudentRecord is a concrete class that implements both interfaces. It means the StudentRecord
inherited all the methods and provide the implementation to the method
of CollegeData and HostelData interface.
interface CollegeData
{
public void collegeDetail();
public void studentData();
}
interface HostelData
{
public void hostelDetail();
public void studentRecord();
}
public class StudentRecord implements CollegeData, HostelData
{
@Override
public void hostelDetail()
{
System.out.println("Hostel Name : RAMA");
System.out.println("Hostel location : KUK");
}
@Override
public void studentRecord()
{
System.out.println("Student selected on based : Percentage, Financial condition");
}
@Override
public void collegeDetail()
{
System.out.println("College Name : DCSA");
System.out.println("College Grade : A");
System.out.println("University of College : KUK");
}a
@Override
public void studentData()
{
System.out.println("courses of Student : MCA, MTECH, MBA, BCA");
}
public static void main (String[] args)
{
StudentRecord obj = new StudentRecord();
obj.collegeDetail();
obj.studentData();
obj.hostelDetail();
obj.studentData();
}
}
Output: College Name : DCSA
College Grade : A
University of College : KUK
courses of Student : MCA, MTECH, MBA, BCA
Hostel Name : RAMA
Hostel location : KUK
courses of Student : MCA, MTECH, MBA, BCA
Let’s discuss the second scenario where we have a common method in both interfaces. When a
concrete class implements both the interfaces and provides the implementation then how
compiler take decision what method should be invoked?
To resolve this problem we will use default methods that was introduced in Java 8. You can
read the default method in detail.
interface CollegeData
{
public void collegeDetail();
default void studentData()
{
System.out.println("courses of Student : MCA, MTECH, MBA, BCA");
}
}
interface HostelData
{
public void hostelDetail();
default void studentData()
{
System.out.println("Student selected on based : Percentage, Financial condition");
}
}
public class StudentRecord implements CollegeData, HostelData
{
@Override
public void hostelDetail()
{
System.out.println("Hostel Name : RAMA");
System.out.println("Hostel location : KUK");
}
@Override
public void collegeDetail()
{
System.out.println("College Name : DCSA");
System.out.println("College Grade : A");
System.out.println("University of College : KUK");
}
@Override
public void studentData()
{
CollegeData.super.studentData();
HostelData.super.studentData();
}
public static void main (String[] args)
{
StudentRecord obj = new StudentRecord();
obj.collegeDetail();
obj.hostelDetail();
obj.studentData();
}
}
Output: College Name : DCSA
College Grade : A
University of College : KUK
Hostel Name : RAMA
Hostel location : KUK
courses of Student : MCA, MTECH, MBA, BCA
Student selected on based : Percentage, Financial condition