Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing an Interface Down the Inheritance Tree Question

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try. . . and you will see Joanne is correct.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic