• 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

interfaceimplementation

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
question taken from Enthuware Test 1

class TestClass implements I1, I2
{
public void m1() { System.out.println("Hello"); }
public static void main(String[] args)
{
TestClass tc = new TestClass();
( (I1) tc).m1(); // line 1
}
}
interface I1
{
int VALUE = 1;
void m1();
}
interface I2
{
int VALUE = 2;
void m1();
}

the folowing question gives o/p as Hello. class TestClass implements both I1 and I2 and both
contain same methods, so how is the call to m1 on line 1, resolved be the JVM ???

i understand that if i say S.o.p( (I1) tc ).m1( ); after line 1, then it will call VALUE=1 of
interface I1. But the implementation of m1( ), does it belong to I1 or I2 ???

please explain
thanks in advance
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear in mind your code formatting please ...

It does not belong to neither of them. An interface is only a definition and does not contain any implementation.

The implementation si provided my the class TestClass, class TestClass has to implement the method m1 as interface I1 and interface I2 requires this.

However if you cast then you can access the variable VALUE from both interfaces.
[ January 02, 2006: Message edited by: Thomas De Vos ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic