Home > Software design >  How exactly multiple inheritance work in Java using interface
How exactly multiple inheritance work in Java using interface

Time:06-10

interface A { void show() }
interface B { void show() }
class C { void cMehod() { System.out.println("cMehod") } 

class Hello extends C implements A,B {

 public void show() {
      System.out.println("cMehod")
 }

 public static void main(){
   Hello h = new Hello();
   h.show();
   h.cMehod();
 }
}

Here you can see I have interface A,B and class c. In Hello class I need to implement method explicitly for interface. so what is use of it. can't we have separate implementation like method in C class and access that method directly in Hello class. without writing it explicitly in Hello class.

CodePudding user response:

This code is the same as the above question code. C class implements A,B

public class Main  extends C{

public static void main(String[] args) {
         Main.cMehod();
        Main m=new Main();
        m.show();
}
   @Override
   public void show() {
       System.out.println("Main");
   }



}
interface A {


 void  show(); 



 }



 interface B{


void show();    


}



class C implements A,B{



static void cMehod()
{ System.out.println("cMehod"); 
    }
@Override
public void show() {
    System.out.println("C");    
}
}

CodePudding user response:

Try to:

A.show() {}
B.show() {}
  • Related