Difference between Inheritance and Interface in Java


Inheritance is a Method to create a hierarchy between multiple classes by replicating some properties from others. There are various types of inheritance present in Java, such as single inheritance, multiple inheritance, multilevel inheritance, hybrid inheritance, and hierarchical inheritance. Interface is the blueprint of a particular class which consists of the constant and abstract class. The interface class allows a machine to apply some specific properties on an object or a class. It is totally an abstract method which helps to perform the Java abstraction on a collection by specifying the behavior of a class. Now the task is here to flag the notable differences between inheritance and interface by using various Java functions.

Input

C . add ( 5, 10 ) ;
C . subtract ( 35, 15 ) ;
C . multiply ( 6, 9 ) ;
C . divide ( 45, 6 ) ;

Output

The result is here. Have A Look : 15.0
The result is here. Have A Look : 20.0
The result is here. Have A Look : 54.0
The result is here. Have A Look : 7.5

Differences Between Inheritance and Interface

Inheritance

Interface

Inheritance allows a base class to pass on the behaviors to its child.

Interface is a class which implements the relationship with all declared methods in a code.

We can reuse the codes from the superclass by using the override method.

It promotes the usability of a code with the method of polymorphism.

It supports only single inheritance with multiple subclasses in a same package.

It supports multiple inheritance without specifying any details of the process.

The inheritance process allows a present class to pass-on the existing characteristics from the super and grandparent class. While, an interface structure is able to define the contract between the abstraction and set methods itself.

Methods Used

Using the Diamond Method

Algorithm

This algorithm we have declared some functions to perform the insertion order in an array list by considering a set value. Moreover, by performing an iteration with a loop we will set a time for the length traversal.

  • Step 1 − Start the process.

  • Step 2 − Declare input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare a public class.

  • Step 5 − Set the functions.

  • Step 6 − Go for the insertion order.

  • Step 7 − Declare an array list and populate it.

  • Step 8 − Declare the Set values.

  • Step 9 − Print the values by following the insertion manner.

  • Step 10 − Declare a loop to iterate the process.

  • Step 11 − Set the timer value.

  • Step 12 − Run the process and get the output value.

  • Step 13 − Terminate the process.

Syntax

In this syntax, we check a treeset first by populate them with some integer values. After that we have declared a map set to create some pairs of identity from those elements to filter them up as per their state by using the entrySet () and hasNext () functions on a table to explain the working of the inheritance and an interface class.

TreeSet < Integer > STTREE = new TreeSet <> () ;
STTREE . add ( 4 ) ;
STTREE . add ( 5 ) ;
STTREE . add ( 6 ) ;
STTREE . add ( 8 ) ;
STTREE . add ( 4 ) ;

IdentityHashMap < Integer, String > ihmap
   = new IdentityHashMap < Integer, String > () ;
ihmap . put ( 10, "ARB" ) ;
ihmap . put ( 20, "RDD" ) ;
ihmap . put ( 30, "ARBRDD" ) ;
ihmap . put ( 40, "KOLDHKA" ) ;
ihmap . put ( 50, "You" ) ;

PRINT THE VALUE HERE ( " IdentityHashMap size : " + ihmap . size () ) ;
PRINT THE VALUE HERE ( " Initial identity hash map :  "     + ihmap ) ;

Hashtable < Integer, String > table  
   = new Hashtable < Integer, String > () ;
table . put ( 1 , "X" ) ;
table . put ( 2 , "Y" ) ;
table . put ( 3,  "Z" ) ; 
table . put ( 4,  "A" ) ;

for ( Map . Entry m : table . entrySet () )
   Iterator < IdentityHashMap . Entry < Integer, String > >
      itr = ihmap . entrySet () . iterator () ;
while ( itr . hasNext () ) {
   IdentityHashMap . Entry < Integer, String > entry
      = itr . next ( ) ;
   TreeMap < Integer,Integer > MAPTREE 
      = new TreeMap <> () ;
   MAPTREE . put ( 2,5 ) ;
   MAPTREE . put ( 3,6 ) ;
   MAPTREE . put ( 4,6 ) ;
   MAPTREE . put ( 2,3 ) ;
}

Using the Diamond Method

In this approach we are going to implement the diamond method to get the differences between inheritance and interfaces. The diamond method of the deadly diamond of death is process which is related to the multiple inheritance. The method add some type of inference to reduce the verbosity of the process.

Example

In this example code, we have created a calculator interface by following the possible methods of inheritance.

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import java.util.*;
interface ARBRDD {
   public void add ( double x, double y ) ;
   public void subtract ( double x, double y ) ;
}
interface Mul_Div {
   public void multiply ( double x, double y ) ;
   public void divide ( double x, double y ) ;
}
interface Calculator extends ARBRDD, Mul_Div {
   public void printResult ( double result ) ;
}
public class MyCalculator implements Calculator {
   public void add ( double x, double y ){
      double result = x + y ;
      printResult ( result ) ;
   }
   public void subtract ( double x, double y ){
      double result = x - y ;
      printResult ( result ) ;
   }
   public void multiply ( double x, double y ){
      double result = x * y ;
      printResult ( result ) ;
   }
   public void divide ( double x, double y ){
      double result = x / y;
      printResult ( result ) ;
   }
   public void printResult ( double result ){
      System.out.println ("The result is here. Have A Look: " + result);
   }
   public static void main ( String args [] ){
      MyCalculator c = new MyCalculator () ;
      c . add ( 5, 10 ) ;
      c . subtract ( 35, 15 ) ;
      c . multiply ( 6, 9 ) ;
      c . divide ( 45, 6 ) ;
   }
}

Output

The result is here. Have A Look: 15.0
The result is here. Have A Look: 20.0
The result is here. Have A Look: 54.0
The result is here. Have A Look: 7.5

Conclusion

Today in this article we have learned about the working process of the various data set methods. With these above mentioned examples, we have performed the process to find the difference between Inheritance and Interface classes present in Java. With the above mentioned logics, syntax and algorithm; we have tried to build Java codes to solve the problem statement and tried to learn about the encountered problem in an efficient way.

Updated on: 19-Oct-2023

590 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements