File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Implementing an Interface Down the Inheritance Tree Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing an Interface Down the Inheritance Tree Question" Watch "Implementing an Interface Down the Inheritance Tree Question" New topic
Author

Implementing an Interface Down the Inheritance Tree Question

Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.

Steve
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 05, 2005
Posts: 3534
    
  15
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.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Try. . . and you will see Joanne is correct.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing an Interface Down the Inheritance Tree Question