• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface trouble

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think there's any simple way. Why do you want to know this information? What are you trying to achieve?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I'm really sorry man....as v cannot have function body in interface...
 
David Peterson
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravinder, unfortunately this won't work because A and B are interfaces and you can't put code into an interface definition.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Everybody's invited. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic