by Dinesh Thakur Category: Inheritance

Multiple inheritance Occurs when a subclass has more than one superclass, whereby it inherits features from all superclasses. Some OOP languages support this, but Java does not. 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. Java uses interfaces to multiple inheritance. Java does not support multiple inheritance. Interfaces serve to implement multiple inheritance. All the methods of an interface are automatically public. (It is not necessary to supply the keyword public when declaring a method in an 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.

The Java interface is used to identify a common set of methods for the group of classes that implement the interface. It is also used to share constants between classes. Interfaces are also used in Java to implement multiple inheritance. Interfaces are used to provide the benefits of multiple inheritance without its implementation difficulties. They allow several classes to share a standard set of methods and constants without requiring these methods and constants to be implemented by a common superclass.

To understand this, let us again consider the Shape class hierarchy as shown in Fig. where the subclasses Rectangle and Circle extend the Shape class.

Multiple Inheritance Part1

Now suppose we want to implement different shapes that not only calculates the area, circumference but also know the position of their center points in the cartesian coordinate plane. One way to do this is to define an abstract class CentredShape and then create subclasses CenteredRectangle and CenteredCircle that extends it.

Multiple Inheritance

In this approach, we notice that we again reimplement area () and circumference () methods that we have already defined in Rectangle and Circle subclasses. So in order to use the already existing methods area () and circumference () of the Shape class, we have to define Centered Rectangle as a subclass of Rectangle so that it inherits area () and circumference() methods. But in Java, a class can directly extend only one super class so it cannot extend both CenteredShape class and Rectangle class. In order to solve this problem, we create CenteredShape interface instead of abstract class.

Multiple Inheritance 3

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)
     {
         super(l,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



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles