This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do we add variables to subclasses while still using polymorphism?

 
Corey Hollaway
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: How do we add variables to subclasses while still using polymorphism? Is there a better option than the one below?

-----------------------
Background: Because we can only have one extends for a class, and the implements(interfaces) can only import final variables, I need to be able to change the variables though.
Because if I want Robodog to have pet variables(like boolean sitting), but Robodog is already extending Robot, then I have to handcode variables into every pet! And thus everyone that creates a Robot Pet would have to agree to a protocol for creates a RoboPet.

-----------------------
One Option: Create an abstract class that extends Robot called: "RobotPet". There I can put all of the pet specific variables & methods inside of. So I'd have:

class Robot{} <-- All Robots go here that aren't pets
class RobotPet extends Robot{} <-- All Robot pets go into here

class Robodog extends RobotPet{}

-----------------------
Interfaces seem so limited! If you can't change the variables (since they're final and must be declared inside of the interface), I have to keep building huge trees of polymorphism if I want variables! Thank you for your time!
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Favour composition over inheritance.

- Peter
 
Corey Hollaway
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I've been reading, and I've noticed that in composition they put instances of classes (that would be inherited if not composition) inside of a class even though it doesn't pass the HAS-A test!
-----------------------------

INHERITANCE:


COMPOSITION:


I like it! It's like if you have a person, and you need that person to have:
-a mouth
-eyes
-teeth

And they all had instance variables! You can't extend them all, because you can only have one direct superclass. You can't use interfaces because their variables are final. So am I getting this ok? Thank you Peter den Haan
[ July 22, 2004: Message edited by: Corey Hollaway ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had no problem with your abstract RoboPet because Robot and RoboPet were both abstrat. But inheritance can get you into trouble. It's very difficult to extend a concrete class without getting into some kind of subtle trouble.

If you're up for some pretty advanced ideas the article Why Extends Is Evil has a good example of one place composition is better than inheritance. Be warned Holub is deliberately outrageous and many people find a lot to argue in his rants, but the lesson that extending concrete classes can get you in trouble is worth while.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the big reasons the OO approach is useful is because it provides us the capability to create intricate structure. This structure is created through composition, where we define relationships between different objects. For example, I could model a business in OO with a single object. That object could have methods that allowed me to figure out what the business does, the name of the business, the address of the headquarters, and get a client list.

Or, much better, the object that models the business could contain objects that keep all of this information in a structured way. For instance, to get access to the client list I might get a hold of the business' internal rolodex object. If I call a method to get the address, that method might return me an Address object. Inside the Address object I might get a hold of a City object that has all sorts of interesting information about the city, major airports, hotels I can stay in, etc.

The amount of detail and the intricacy of the structure that is provided in the application should be driven by the purpose of the application. If you create a Dog class, it's going to have different behaviors and structure internal data in a different way if that class is being designed for a Sims-style game than if it's being used in a veterinarian's office software. But the key to building good OO systems is recognizing how to structure these relationships between objects.

While composition can represent several different kinds of relationships, inheritance can model only one. I've seen people make the mistake of trying to have an Engine class extend an Automobile class, simply because they both have start() methods. Nope...as you now know, a car HAS-A engine, but neither one IS-A kind of the other.

sev
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic