aspose file tools*
The moose likes Beginning Java and the fly likes Interface trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface trouble" Watch "Interface trouble" New topic
Author

Interface trouble

Anirudh Chandrakant
Greenhorn

Joined: Mar 23, 2004
Posts: 5
hi,
im sorry, i have no clue bout this problem...so dont know which category it belongs to, so taking the safe route out and posting it in begginer's category, cos thats who i am. i have this basic problem with interfaces. i have been stuck for a while. i Have two interfaces A and B declaring the same function prototype get(). a class C implements these two interfaces and defines get(). Now in the main function when i call a.get() (a is an object of A) or b.get() (where b is an object of B)...is there any way to find out which interface called the function?..chk out the code below to understand my problem better.
interface A {
void get();
}
interface B {
void get();
}
class C implements A,B {
public void get(){
System.out.println("Hello");
}
}
public class inter {
public static void main(String[] args) {
C c = new C();
A a;
B b;
a = c;
a.get();//At this point
b = c;
b.get();//And at this point
}
}
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
I don't think there's any simple way. Why do you want to know this information? What are you trying to achieve?
Ravinder S Edhan
Ranch Hand

Joined: Dec 15, 2003
Posts: 57
Hi....
If you want to check that whether correct functions are called... what u can do put an SOP statment in ur function....
Like in first interface...
function get() {
System.out.println("Interface A");
}
function get() {
System.out.println("Interface B");
}
Bye...and this will worl for u
Ravinder S Edhan
Ranch Hand

Joined: Dec 15, 2003
Posts: 57
Hi..
I'm really sorry man....as v cannot have function body in interface...
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
Ravinder, unfortunately this won't work because A and B are interfaces and you can't put code into an interface definition.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I'm pretty sure,that on identical interfaces, JAVA will call the first one, and only the first one. JAVA is smart enough to realize that they both do the same thing. I could be wrong on this one. I remember reading it somewhere.
HTH.
Gabe
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Implementing an interface is merely a contract to include that interface's methods in that class with the implementation clause. After that other code can cast your class to any the implemented interfaces and be able to link and call the method(s) you implemented from that interface. The interface itself is never called, it is merely the "middle man" used at compilation time to ensure the caller can find the interface's methods in the implementing class.
The fact that you are implementing two interfaces with only one method is fortuitous since calling code can cast your implementing class to either interface and still call the method.
Hope that is clear. Ranchers smarter than me might care to elucidate.
Ed


The nice thing about Standards is that there are so many to choose from!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interface trouble