| Author |
multiple interface
|
Jitendra Jha
Ranch Hand
Joined: Jan 28, 2007
Posts: 91
|
|
i have made 2 different interfaces which have the same method prototype(method declaration).when i implement both in a class,i see that they work fine.i want to know which method is used(from interface 1 or from interface 2)?how do i check? package Dump1; interface interfaceExp { int a=1; void Name(); } package Dump1; interface interfaceExp1 { int a=1; void Name(); } package Dump1; class interfaceExpdemo implements interfaceExp,interfaceExp1 { public void Name() { System.out.println("Hello"); } public static void main(String args[]) { interfaceExpdemo ied=new interfaceExpdemo(); ied.Name(); } }
|
Jitendra
SCJP1.5
SCWCD1.5
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16689
|
|
i want to know which method is used(from interface 1 or from interface 2)?how do i check?
Unfortunately, there is no "which method" as there is only one implementation, that satifies both interfaces. From the method, there is no mechanism to tell what the reference type that was used to call the method was. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
|
and more importantly, it doesn't matter. The interfaces don't have any implementation, they are only a "contract" that the class that does implement the interface will have the contained method signature.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
Originally posted by pete stein: and more importantly, it doesn't matter
Yet it may. If interface A expects method xyz() to do one thing, and interface B expects it to do another, the contract with one or other of the interfaces needs to be broken. It doesn't matter as far as the compiler is concerned, but the situation is still one to be avoided.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
Originally posted by Bear Bibeault: Yet it may. If interface A expects method xyz() to do one thing, and interface B expects it to do another, the contract with one or other of the interfaces needs to be broken. It doesn't matter as far as the compiler is concerned, but the situation is still one to be avoided.
Thank you for that clarification.
|
 |
Jitendra Jha
Ranch Hand
Joined: Jan 28, 2007
Posts: 91
|
|
|
In the light of the above discussion,the question still remains,how do we know which method is being used.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
|
As there can be only one method, there is no amibiguity. What is ambiguous is which intention the method implements, which is why the situation shold be avoided.
|
 |
 |
|
|
subject: multiple interface
|
|
|