aspose file tools*
The moose likes Beginning Java and the fly likes multiple interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiple interface" Watch "multiple interface" New topic
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: 18842
    
  40

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: 61218
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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: 61218
    
  66

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple interface