aspose file tools*
The moose likes Beginning Java and the fly likes adding interfaces to a superclass/subclass relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding interfaces to a superclass/subclass relationship" Watch "adding interfaces to a superclass/subclass relationship" New topic
Author

adding interfaces to a superclass/subclass relationship

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I'm just trying to get my head around the following...

Suppose class B extends class A. Suppose also that class A implements interface I.

So I can be used as a reference type for an object of class A.

From my research it seems that this property does not extend to B. If we want to use an I reference type for an object of class B, then B must implement I also. I other words a subclass does not inherit an superclass's implemented interface

I say this because there are examples in the Sun java class library where B extends A, and both A and B implement I.

OK I have no problem dealing with this, I was just wondering why it was so.

thanks.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Wrong.

Think about it.

If A implements I, then A is a I. And if B extends A, then B is a A and since A is a I, B is a I too.

Confusing but concise


JDBCSupport - An easy to use, light-weight JDBC framework -
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Sebastian Janisch wrote:Wrong.

Think about it.

If A implements I, then A is a I. And if B extends A, then B is a A and since A is a I, B is a I too.

Confusing but concise


your meaning is clear enough. But I am missing something, here is an example.

there is an abstract class, in package java.awt.geom I believe, called Line2D. Line2D has two subclasses Line2D.Double, and Line2D.Float. All three class are documented as implementing the interface Shape.

So, if what you say is true, what's the point of the two subclasses implementing Shape?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

They may implement it in different ways.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
David Newton wrote:They may implement it in different ways.


hmmm ok I'm gonna have to think on this. Your answer makes sense to me in a way, but I'm still unclear on the why.

It's not exactly clear to me what role is played by the fact that the subclasses are nested and static. I'm not sure that is relevant to my original question.

anyways it's the sort of relationship that exists with a lot of the basic shape classes in package java.awt.geom. There is an abstract superclass for the shape, then 2 nested static concrete classes that are defined by float and double parameters respectively. Then the draw method of the Graphics2D class uses the Shape type. And Shape is the interface implemented by both.

hmm...I suppose this sort of relationship allows us to add another layer of abstraction, and perhaps allows us to refine our shapes by adding precision, without having to change method calls, just the object instantion. This in addition to the other layer of abstraction which allows us to deal with different shapes in the same way.

Anyways, just thinking out loud here. I'll have to give it time to sink in.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
It also means that anything implementing the Shape interface will have 10 methods about whether it intersects another Shape, etc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: adding interfaces to a superclass/subclass relationship