by Dinesh Thakur

Unlike other OOP languages like C++ that include the concept of multiple inheritance (where class directly inherits two or more classes), Java only supports single inheritance. The use of multiple inheritances makes the programming language for more complex to use, learn and implement. The designers of Java wanted to make the language simpler and free from any complication and ambiguity. So the Java designers decided they could do without it. But while working with programs in Java, we often come across situations where we want multiple inheritances.


Java provides solution to this problem using the concept of interface. Although Java class can extend only a

single superclass, it can implement any number of interfaces. Inheriting multiple interfaces is not same as true multiple inheritances. Interface approach only enables you to inherit method declarations and not implementations. So if a class implements multiple interfaces then class must provide the implementation for all the methods declared in the interfaces.

abstract class Shape

      abstract double area();
      abstract double circumference();
class Rectangle extends Shape
    private double length,breadth;
    Rectangle(double x,double y){length=x;breadth=y;}
    public double getLength() { return length;}
    public double getBreadth() {  return breadth;}
    public double area() { return(length * breadth);}
    public double circumference(){ return (2*( length + breadth));}
   class Circle extends Shape
     private double radius;
     Circle (double r){radius=r;}
     public double getReatius() { return radius;}
     public double area() { return (Math.PI * radius * radius);}
     public double circumference() { return(2* Math.PI * radius);}
   interface CenteredShape
     public void setCenter(double x, double y);
     public double getCenterx();
     public double getCentery();
   class CenteredRectangle extends Rectangle implements CenteredShape
     //New intance fields
     private double centerx, centery;
     // A constructor
     public CenteredRectangle(double cx, double cy, double l, double b)
         centerx = cx;
         centery = cy; 
     //interit all the methods of Rectangle,but must
     //provide implementstions of all the Centerd methods.
     public void setCenter(double x,double y) { centerx =x; centery = y; }
     public double getCenterx() { return centerx;}
     public double  getCentery() { return centery;}
   class InterfaceMultipleInheritence
       public static void main(String[] args)
         Shape s; //Shape class reference variable
       CenteredRectangle r = new CenteredRectangle(2,3,4,5); 
       s = r; //Assign rectangle reference to shape reference
       System.out.println("Area of Centered Rectangle is = "+s.area());
       CenteredRectangle rl = (CenteredRectangle)s; //downside casting
       System.out.println("Coordinates are = ("+rl.getCenterx()+","+rl.getCentery()+")");

Interface Multiple Inheritance Java Example