The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Interfaces : Multiple Inheritance

In the section on Inheritance, we have seen that Java doesn't support multiple inheritance ( a subclass extending multiple super classes ). This limitation is addressed using the concept of interfaces. A subclass cannot extend multiple super classes but it can implement multiple interfaces.

What is an interface ?
An interface is like a class containing methods and variables but the difference is that an interface can define only abstract methods and final fields. Interfaces doesn't contain constructors and they cannot be instantiated. There is no method definition inside the interface. It is the responsibility of the class that implements the interface to define those methods. If the class that implements an interface doesn't define all the methods of the interface, then that class becomes an abstract class and cannot be instantiated. Following program illustrates the use of interfaces to achieve multiple inheritance :

/* define an interface named Vechile */
interface Vechile {
   public void vechileInfo(); // abstract method ( no method body )
}

/* define an interface named Car */
interface Car {
   public void carInfo(); // abstract method
}

 /* Ferrari inherits properties of both Vechile and Car classes */
class Ferrari implements Vechile, Car {
   public void vechileInfo() { // define abstract method of Vechile class
      System.out.println("I am a Vechile");
   }
   public void carInfo() { // define abstract method of Car class
      System.out.println("I am a Car");
   }
   void ferrariInfo() {
      vechileInfo();
      carInfo();
      System.out.println("My name is Ferrari");
   }
}

public class InterfaceDemo {
   public static void main(String args[]) {
      Ferrari f = new Ferrari();
      f.ferrariInfo();
   }
}

A class can extend another class and implement any number of interfaces at the same time. Following program illustrates another example of multiple inheritance.

/* define a class called Employee */
class Employee {
   protected int emp_id; // employee id
   protected int dept_id; // department id
   Employee() { }
   Employee(int id1, int id2) {
      emp_id = id1;
      dept_id = id2;
   }
   void employeeInfo() {
      System.out.println("Employee ID : " + emp_id);
      System.out.println("Dept. ID : " + dept_id);
   }
}

/* define an interface named Programmer */
interface Programmer {
   final static double hikePercent = 13.5;
   void programmerInfo();
}

/* 'Engineer' extends 'Employee' class and implements 'Programmer' interface */
class Engineer extends Employee implements Programmer {
   private String name;
   private int lang;
   private double salary;

   Engineer() { }
   Engineer(String emp_name, int emp_id, int dept_id, int lang_id) {
      super(emp_id, dept_id); // call super class constructor
      name = emp_name;
      lang = lang_id;
   }

   void setCurrSalary(double sal) {
      salary = sal;
   }

   /* define the abstract method of 'Programmer' class */
   public void programmerInfo() {
      if (lang == 1) {
         System.out.println("Java programmer");
      }
      else {
         System.out.println("C++ programmer");
      }
   }

   /* print all the information about an engineer */
   void engineerInfo() {
      System.out.println("Employee Name : " + name);
      employeeInfo();
      programmerInfo();
      double hike = ( salary * hikePercent ) / 100; // compute hike
      System.out.println("Hike : " + hike);
   }
}

public class ExtendImplementDemo {
   public static void main(String args[]) {
      Engineer e = new Engineer("John", 6723, 2, 1);
      e.setCurrSalary(72760.67);
      e.engineerInfo();
   }
}

Extending Interfaces
We can create a subinterface by extending another interface. This behaviour is similar to that of classes but an interface can inherit from multiple interfaces. Following program illustrates how to extend interfaces :

interface X {
   final int i = 175;
   void methodX();
}

interface Y {
   final float j = 11.56f;
   void methodY();
}

interface Z extends X, Y { // extending multiple interfaces
   final String k = "csegeek";
   void methodZ();
}

class A implements Z {
   public void methodX() {
      System.out.println("i = " + i);
   }
   public void methodY() {
      System.out.println("j = " + j);
   }
   public void methodZ() {
      System.out.println("k = " + k);
   }
   void methodA() {
      methodX();
      methodY();
      methodZ();
   }
}

public class ExtendInterfaceDemo {
   public static void main(String args[]) {
      A obj = new A();
      obj.methodA();
   }
}

What if a class extends 2 interfaces having same abstract method ?
Since the implementation of abstract method is provided by the class which implements the interfaces, there is no ambiguity even if two interfaces declares same abstract method. Following program illustrates this concept :

interface X {
   void display();
}

interface Y {
   void display();
}

class A implements X, Y {
   public void display() {
      System.out.println("Ambiguity Resolved");
   }
}

public class ResolveAmbiguity {
   public static void main(String args[]) {
      A obj = new A();
      obj.display();
   }
}

Back | Next