jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes How do we add variables to subclasses while still using polymorphism? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do we add variables to subclasses while still using polymorphism?" Watch "How do we add variables to subclasses while still using polymorphism?" New topic

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

Corey Hollaway

Joined: Jul 21, 2004
Posts: 11
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
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Favour composition over inheritance.

- Peter
Corey Hollaway

Joined: Jul 21, 2004
Posts: 11
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!



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

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

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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.

I agree. Here's the link: http://aspose.com/file-tools
subject: How do we add variables to subclasses while still using polymorphism?
It's not a secret anymore!