• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

adding interfaces to a superclass/subclass relationship

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They may implement it in different ways.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also means that anything implementing the Shape interface will have 10 methods about whether it intersects another Shape, etc.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic