Home Programming Java How to achieve multiple inheritance in java?

How to achieve multiple inheritance in java?

899
0

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B.

But java does not support multiple class inheritance. To overcome this problem we use interfaces to achieve multiple class inheritance. With the help of the interface, class C( as shown in the above diagram) can get the features of class A and B.

Example :

import java.util.Scanner;
 class A{
         int a,b;
         Scanner obj;
         void get(){
             obj = new Scanner(System.in);
             System.out.println("Enter two numbers: ");
             a = obj.nextInt();
             b = obj.nextInt();
         }
     }
 interface B{
     void sum();
 }
 class C extends A, implements B {
     int sum;
     void sum()
     {
         sum = a+b;
     }
     void display()
     {
         System.out.print("Sum of two numbers: "+sum);
     }
 }
 public class multipleInheritance {
     public static void main(String args[]){
         C ob = new C();
        ob.get();
        ob.sum();
        ob.display();
     }
 }

LEAVE A REPLY

Please enter your comment!
Please enter your name here