• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Implementing an Interface Down the Inheritance Tree Question

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The following code compiles:

My questions are:
1) Even though Test2 implements Iface, why doesn't it need to provide implementation for the method implementMe() ?
2) Even if I do provide a method with the same signature and return type as implementMe(), is it really an implementation of the interface method?
3) What am I gaining (if anything) by adding "implements Iface" to the Test2 class"?

Thank you very much,
-Russ
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Russ Russell wrote:...
My questions are:
1) Even though Test2 implements Iface, why doesn't it need to provide implementation for the method implementMe() ?


Test2 inherits an implementation of the implementMe() method from its parent class, Test1. Therefore it does not need to re-implement it.

2) Even if I do provide a method with the same signature and return type as implementMe(), is it really an implementation of the interface method?


Yes.

3) What am I gaining (if anything) by adding "implements Iface" to the Test2 class"?


You are clearly telling anyone who uses the class Test2 that it is a valid and intended implementation of Iface. I generally do not do that, preferring to only add the implements clause on the class which actually does do the method implementation.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Steve.

It seems unnecessary to me to add the implements clause if this class is not explicitly providing implementation. However I saw an example similar to this in a sample certification question, and I just wanted to understand why anyone would do such a thing.

Thanks,
-Russ
 
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to add the "implements" clause to specify that the class implements the interface. That's what the "implements" clause is for, no more and no less. If you don't declare that a class implements an interface, then it doesn't implement that interface.

That all may seem rather trivial, and it is. However from that one trivial statement you should be able to infer why somebody would want to "do such a thing", as you put it. Unless you were asking why somebody would want to implement an interface, that is...
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You need to add the "implements" clause to specify that the class implements the interface.


No you don't. If a class extends another class that implements the interface there is no need to have an explicit implements clause in it's declaration.
If you remove the implements Iface from the Test2 declaration then Test2 still implements Iface. That is what was being asked.

 
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try. . . and you will see Joanne is correct.
 
Campbell Ritchie
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It’s all part of inheritance. If a Test1 IS-An IFace, and a Test2 IS-A Test1, then a Test2 IS-An IFace, too.
 
eat bricks! HA! And here's another one! And a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic