Difference between Interface and Inheritance (with Comparison Chart and Example)

Interface vs Inheritance Interfaces used to enable several distinct classes to share a probable group of properties and methods. The Interface is useful for abstracting a class where it specifies the operation performed by the class but does not reveal the background working or how it is going to do it.

While inheritance assists the creation of specialized subclasses by base class and this is helpful in reusing the code. More generally inheritance is used to extend the class functionality. However, both are the concepts of object-oriented programming offer polymorphism feature.

Content: Interface and Inheritance

  • Comparison Chart
  • Definition
  • Key Differences
  • Example
  • Conclusion
  • Comparison Chart

    Basis for comparisonInterfaceInheritance
    BasicDerives new classes by using an existing class.Employs class abstraction and multiple inheritance.
    ProvidesAbstraction and multiple inheritance.Reusability
    MembersDeclared as constant.Declared as constant and variable.
    Class definitionContain the abstract methods.Contain the code for each of its methods.
    Access specifiers usedPublicPublic, private or protected.
    InstantiationCannot be used to declare objects.Class can be instantiated using objects.

    Definition of Interface

    Interfaces are kind of similar to classes but they contain methods with no code within it. These methods are known as the abstract method which lacks instance variables or the methods are declared without a body. It is essentially used to abstract a class. The interface is defined once and implemented in by several classes and in different ways. So, when a class implements an interface it is compelled to implement all the methods that the class contains. As mentioned above Interface supports polymorphism, it can employ “one interface multiple methods”.

    The methods can also be resolved during runtime using interfaces. An Interface provides abstraction. There is a restriction on Java, it does not enable multiple inheritance and only a single class can inherit another class. To achieve multiple inheritance, the interfaces are used, so that we can make multiple classes to inherit its properties into a subclass.

    Definition of Inheritance

    Inheritance is the concept used to create hierarchical frameworks. The inheritance enables to construct a general class that define properties common to a collection of linked entities, and this general class can be inherited by other classes further including unique properties to its classes. The terms used in java are superclass and subclass, superclass for the class that is going to be inherited while the class that does the inheriting is referred to as a subclass. Thus, the subclass is said to be the specialized edition of the superclass. The subclass inherits all the defined instance variables and methods of the superclass and also includes its own unique components to it.

    To interpret the relationship between the objects and classes, let’s assume there is a class named as Snake the one instance of the class shows only one individual snake, but when we talk about class snake it will involve the features of a snake (what makes it a snake?). Snake objects share the same abilities to crawl, prey, etc. Now if we want to include more classes such as turtle and frog, then the turtle and frog classes will have their own methods such as swim and hop respectively. But the turtle, frog and snake share the same property of prey. So, these classes can extend a superclass named as reptile which contains the method prey.

    Key Differences Between Interface and Inheritance

  • The inheritance concept permits the subclasses to inherit the properties of the base class. On the other hand, an interface is used to implement the abstract class and multiple inheritance.
  • The merit of using inheritance is that it enables the same data to be used again and again, thus conserving the storage space and reduce the code complexity. As against, the interface provides abstraction and multiple inheritance.
  • Interface definition involves only constants. Conversely, While inheriting the class the members can be declared as constant and variable.
  • An interface contains the abstract methods while inheriting classes contain code for each method.
  • Access specifiers used in an interface can be only public. In contrast, we can use any specifier among private, public or protected during inheritance.
  • The class is instantiated by declaring objects in inheritance. On the contrary, the interface is not used to declare objects, the prior work of an interface is to be inherited a class.
  • Example of Interface

    In the below given program, an interface is created at first named as Volume and implemented in the two different classes Cuboid and Sphere. The instances of each class is created using new operator and object of type Volume interface class is declared. Now, the reference to the Cuboid object c to v (Volume object) is assigned. At last, the compute method of Volume and Cuboid is called and the same procedure is followed for calculating the sphere volume.

    interface Volume // interface defined { final static float pi = 3.14F; final static float q = 1.33F; // q=4/3 float compute (float x, float y, float z); } class Cuboid implements Volume // interface implemented { public float compute (float x, float y, float z) { return (x*y*z); } } class Sphere implements Volume { public float compute (float x, float y, float z) { return (q*pi*x*x*x); } } public class InterfaceEx { public static void main (String args[]) { Cuboid c = new Cuboid(); Sphere s = new Sphere(); Volume v; // interface object v = c; // v refers to cuboid object c System.out.println("Area of Cuboid = " + v.compute(11,9,8)); v = s; // v refers to sphere object s System.out.println("Area of Sphere = " + v.compute(10,0,0)); } }

    Example of Inheritance

    The example shown below is a single inheritance, there is a superclass class named as X and a subclass extending the properties of the base class. To use the data of the superclass we are invoking the objects in the other class named as InheritanceEx.

    class X { int a, b; void display1 () { System.out.println ("a and b: " + a + " " + b); } } // Create a subclass by extending class A. class Y extends X { int c; void display2 () { System.out.println ("c: " + c); } void add () { System.out.println ("a+b+c: " + (a + b + c)); } } public class InheritanceEx { public static void main (String args[]) { X base = new X (); Y derived = new Y (); // The superclass may be used by itself. base.a = 15; base.b = 25; System.out.println ("Contents of base class : "); base.display1 (); System.out.println (); /* The subclass has access to all public members of its superclass. */ derived.a = 11; derived.b = 22; derived.c = 33; System.out.println ("Contents of derived class : "); derived.display1 (); derived.display2 (); System.out.println (); System.out.println ("Sum of a, b and c in derived class :"); derived.add(); } }

    Conclusion

    The inheritance and interfaces provide the ways to add new classes to the already existing program without altering it. Although there is a huge difference between inheritance and interface, java does not allow a class to be inherited from two different class, in other words, a class can inherit only a single class. While the interface is created to eliminate this disadvantage and allow multiple inheritance.

    ncG1vNJzZmislZi1pbXFn5yrnZ6YsrR6wqikaJyZm7OmvsSnmp5lkprBuLHEp2SipqSav6etwp5kmqaUYravtMSroK2Znpiyb7TTpqM%3D