• 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

Mock exam question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a simple question from JQ+ mock, but it has been bothering me. :
What, if anything, is wrong with the following code?
// Filename: TestClass.java
class TestClass implements T1, T2
{
public void m1(){}
}
interface T1
{
int VALUE = 1;
void m1();
}
interface T2
{
int VALUE = 2;
void m1();
}
options are:
1.TestClass cannot implement both as it leads to ambiguity.
2.The code has not problems
3.The code will work fine if VALUE is removed from one if the interfaces.
4.The code will work fine if m1() is removed from one of the interfaces.
5. None of the above.
--My answer is 4.
--I haven't tried to run it but from what i understand about interfaces it should not be able to evaluate which m1() method to call?
The answer from JQ+ says :2.
Please help me , if i am missing anything here!
Pratibha.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer 2 is in fact correct.
Interfaces do not give any clue about implementation, they just tell which method should be implemented by classes wanting to be_like_a Interface. In fact, in this case if TestClass implements the body of void m1() it's ok, what would be wrong ???
When a class implements a method of an interface it does not invoke the method of the interface, so it does not have to bother with "What method should I invoke the one of T1 or the one of T2 ? This does not make sense.
T1 declares having a method void m1() and T2 also. So TestClass implements T1 and T2 so it has to provide an implementation for void m1() and that's it.
To access VALUE in TestClass you have to qualify that access (i.e. T1.VALUE or T2.VALUE) and everything works fine
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pratibha
Remember that in an interface all methods are implicitly abastract so you have to override them anyway, so it doesn't really matter that they both have the same method name. Where you would run into a problem is using the variable VALUE. If you try to use that in the class that implements both of the interfaces you'll have to qualify it using the name of one of the interfaces:

hope that helps you out

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Pratibha Enjeti
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Val & Dave,
I understand it better now.
Thank you for your time in replying back.
Pratibha.
 
reply
    Bookmark Topic Watch Topic
  • New Topic