Issue as regards multiple inheritance in java -


java said not support multiple inheritance 1 can implement more 1 interface. in case of snippet below of show() method implemented.

public interface i1{     public void show();  }   public interface i2{     public void show();  }   class mine implements i1, i2{      @override     public void show(){         //do      } } 

my question how know show() method overridden.

java doesn't have equivalent c#'s public void a.show(), cause doesn't let interfaces conflict in way. if implement 2 interfaces declare method same name , same argument types, either have same return type, or java not compile code. , once return type also same, class implements 1 or other implements both simultaneously, signature satisfies requirements of both interfaces.

of course, if want verify...

public class example {     interface { public void show(); }     interface b { public void show(); }      class c implements a, b {         public void show() { system.out.println("show()ing"); }     }      public static void main(string[] args) {         c c = new c();          a = c;         a.show();  // something, trust me :p          b b = c;         b.show();  //     } } 

c's void show() satisfies both interfaces' method declarations, all's well, , 2 lines appear in console (one call through reference, 1 b call).

now, had

interface { public void show(); } interface b { public int show(); }  <-- different return type!  class c implements a, b { public void show(); } 

it's obvious method you're trying implement. wouldn't work, though -- , can't, because java won't let 2 interfaces' methods exist in same class.

now, 1 last example.

interface { public void show(); } interface b { public int show(int); }  <-- different return type!  class c implements a, b {     public void show() { system.out.println("showing..."); }     public int show(int i) { system.out.println(i); } } 

this fine. since 2 interfaces declarations share name , not arg types, java lets them coexist in c. requires implement 1 method each interface.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -