View Single Post
  #15 (permalink)  
Old 07-11-2009, 07:42 PM
Fubarable's Avatar
Fubarable Fubarable is offline
Moderator
 
Join Date: Jun 2008
Posts: 6,431
Rep Power: 8
Fubarable is on a distinguished road
Default
Originally Posted by DCC1 View Post
The PongBall class extends the PongBall to get access to the getHeight() and getWidth() methods that pertain to the PongPanel class (dimensions of the window).
The question you need to ask yourself is: is a PongBall a PongPanel? In other words does it fulfill an "is-a" relationship (google this term)? Answer: it does not. For example, a class Dog would fulfill an is-a relationship with an Animal class, but not with a DogHouse class, so Dog can extend Animal but should never extend DogHouse. Another important concept is that of the "has-a" relationship. DogHouse does fulfill a has-a relationship with Dog, and thus it can hold Dog objects. PongPanel likewise can fulfill has-a with PongBall and can hold these objects as fields of the class.

Point number two, making it subclass PongPanel wouldn't automatically give it the width and height since it is not the same object as the "true" PongPanel. Inheritance is not the way to pass this information.

And final point: PongBall doesn't even need to know this information. Does a physical ball know where the ground is? Nope, it only knows when it collides with something. So give your PongBall class a boolean method contains(int x, int y), to see if a point is contained in the ball or not. That way the object that holds the balls can easily test for collisions.

Quote:
Is this not the correct way to do this? Might it make more sense to not extend PongPanel and not use those methods?
precisely

Quote:
By circular reference, is that similar to a Singly Linked Circular List?
Nope, I mean you have a PongPanel that contains PongBalls, and these subclass PongPanel, and this can contain PongBalls, and these subclass PongPanel...
It can get downright ugly here.

Last edited by Fubarable; 07-11-2009 at 10:14 PM.
Reply With Quote