IntelliJ Java IDE
The moose likes OO, Patterns, UML and Refactoring and the fly likes Perils of Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Perils of Inheritance" Watch "Perils of Inheritance" New topic
Author

Perils of Inheritance

Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
This has been discussed in Meilir Page Jones book.I have started this thread to discuss an excellent example from his book.
Can you figure out what's wrong in the following inheritance heirarchy and if there is something wrong, what should we do to correct it?
<pre>
---- ------------------
Bear EndangeredSpecies
---- ------------------
^ ^
| |
| |
| |
--------------------
P A N D A
--------------------
</pre>

-- Sandeep
[This message has been edited by Desai Sandeep (edited July 05, 2001).]


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
A potential problem would be that any child class of "Panda" would inherit from "Bear" and "Endangered Species" but what if the child class was not endangered?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Thomas,
Yes,that's correct.
Meilir Page-Jones suggests to consider instances of the 3 classes in question to find errors in the heirarchy.For example instances of Panda are An-An, Chu-Chu, Ling-Ling, Miou-Miou, Oh-Oh, etc.The instances of Bear are Yoji, Teddy, Winnie, Paddington, Fred, etc.However,the instances of EndangeredSpecies are entire species,such as snub-nosed muskrat,the lesser spotted tree-gripper,the reasonably large cat of Sumatra, the unreasonably large cat of Borneo, etc.
In other words, two of the classes (Panda and Bear) have individual animals as instances, whereas the third (EndangeredSpecies) has whole species as instances.It would be correct to say, "Ling-Ling is an instance of Panda" or "Ling-Ling as a Panda is also a instance of a Bear".However, it is not correct to say "Ling-Ling is an instance of EndangeredSpecies".Thus, Panda could inherit from Bear but not from EndangeredSpecies.
With this analysis, what then should be the correct heirarchy diagram?
-- Sandeep
[This message has been edited by Desai Sandeep (edited July 06, 2001).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The heirarchy would be
Animal <--- (maybe a few others here)Bear <--- Panda
EndangeredSpecies actually would not be in the heirarchy chain. It would be IMHO be a collection of Animals anyone of which could be added or removed at anytime.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
Carl,
I agree with you that EndangeredSpecies would not be a part of the hierarchy chain. However, I don't think they are a collection either. Are you saying three instances of Panda can be added and one instance of bear can be removed from this collection? you must be kidding.
I consider EndangeredSpecies a separate class which has no relation to Animal class. They could be birds, plants, honest politicians, Algol programmers etc.
-Ramesh
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Hi
I kind of agree with Carl when he says that Endangered Species could be a collection of animals. Why not, even if you have 3 diffrent Pandas and 1 Bear in this list. They still can be considered endangered.
Does not the original example show multiple inheritance> Wasn't this supposeed to be replace by Interfaces? If so, could EndangeredSpecies be an interface?
I am doubting it myself as in this context it would be too inflexible since you would have to change the class definition if the status of the species/animal changed.
Taking that last comment into account, then would it be much more simple to just make "Endangered" a protected boolean in the animal class? Then you have the flexibility to make an (Animal), (Bear) or (Panda) endangerd and it's easier to turn off and on?
If so, then I disagree that there are "Perils with Inheritance" because the model was actually wrong and not the methodology.
Maybe I'm doing too deep into the nitty gritty. But I get carried away once I get going at times.
:P

------------------
SOURCE CODE should be SURROUNDED by "code" tags.
Click here for an example
[This message has been edited by John Bateman (edited July 06, 2001).]


SOURCE CODE should be SURROUNDED by "code" tags.
Junilu Lacar
Ranch Hand

Joined: Feb 26, 2001
Posts: 3008
Is there anything wrong with making EndangeredSpecies a boolean class attribute? That would be my first thought anyway...


Junilu
[How to Ask Questions] [How to Answer Questions] [MiH]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Good arguements. I still think EndangeredSpecies would be a container class. However, it shouldn't contain Objects of each species but, rather the Class it self so it would contain a Class[].
A field would do to determine if a species of animal is endangered but, it would need to be static and static's are not inhereted. Perhaps Animal should implement an Inerface such as 'EndangeredStatus' which can have a static method getEndangeredStatus() this will ensure each subclass can determine it's own implementation of the interface.
My 2 cents
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Great!People think pretty fast here.Let me add something too..
I believe all of us agree on this heriarchy :
<pre>
BEAR
^
|
|
{ incomplete }
|
|
PANDA
</pre>

{incomplete} tells us that Panda is not the only kind of Bear
Also, with our discussions we realize that EndangeredSpecies represents something different than the individual animals; it represents a kind of Species
Hence, let's have this class heirarchy
<pre>
SPECIES
^
|
{disjoint,complete}
|
------------------------------------------------
| |
| |
| |
ENDANGERED NONENDANGERED
SPECIES SPECIES
</pre>

The two subclasses EndangeredSpecies and NonEndangeredSpecies represent mutually exclusive subsets of a complete set of species.
Now the question is "How do we link the two diagrams together?" or rather "How do we design the fact that Panda and some other species are endangered?"
Continue the discussion,
Sandeep
[This message has been edited by Desai Sandeep (edited July 06, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I don't see ENDANGERED SPECIES being a collection because it would have its own attributes (date species became endangered, level of endangerment, estimated number of living individuals, etc) plus methods supporting these attributes.
You could have BEAR have a SPECIES attribute which could be an ENDANGERED SPECIES object. Or better yet, move the SPECIES attribute far up the hierarchy to LIVING THINGS.
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100

John,
If I have two white tigers in my universe, Can I add one of them to the EndangeredSpecies collection and leave the other? As Carl said individual objects can not be allowed to be added/removed from the collection.
My suggestion :




[This message has been edited by Ramesh Donnipadu (edited July 06, 2001).]
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Hi
(Love this topic by the way)
I think if we stick to the Endangered and NonEndangered examples we are starting (or I am anyway) to see that the Species 'title' for the Parent Class should maybe be renamed to SpeciesStatus and then incorporated into our Animals objects in a HAS-A manner instead of an IS-A method.
The reason I say this is because I think we are making the mistake of directly putting our way of verbalizing the status of a species int our model. I.E. Endangered and NonEndangered are NOT really species, but they are 'States' of a Species.
I would like to bring this example into play
<PRE>
Animal
^
|
| (generalisation)
|
---------------------
| |
WarmBlooded ColdBlooded
^ ^
| | (generalisation)
| |
| |
BirthTheirYoung LaysEggs
^ ^
| |
| | (generalisation)
| |
Dog Lizard
</PRE>
No joke now, but how does the Platypus fit into this model?
It's warm blooded but it lays eggs? this means that not ALL warmblooded animals BirthTheirYoung. Is this another Peril of Inheritance in that it is really too strict in it's method of classification? Or could it be the problem with our understanding of how we classify our animal life?
In other words, is the problem with the model or the methodology?
In this case I keep asking myself if we keep the WarmBlooded and ColdBlooded classification and use the BirthingMethod as an attribute of an instance?!?

[This message has been edited by John Bateman (edited July 06, 2001).]
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Ramesh
Makes sense when you put it that way.
Please understand that I am just really getting into architecture and I have alot of misconceptions about how it's all put together.
But (sorry for sounding repetitive), I tend to have a problem with something like the implementation of "NonEndangeredSpecies" Because if that status happen to change and you need to switch the 'functionality' then you would have to change/update all the old Panda classes definitions, and this is something that I don't think I would want to do.

Junilu Lacar
Ranch Hand

Joined: Feb 26, 2001
Posts: 3008
Would it help to put this into some sort of context? When trying to model something, you usually only capture those aspects of the real world that pertain to the problem at hand. Otherwise, you could go on and on coming up with solutions that are looking for problems. So, what is it about bears and pandas and endangered species that we are interested about?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Excellant point JUNILU,
Everyone of us has provided answers to a question that we don't know! We missed 'What is the fundamental purpose of the Application?' If we can answer this, then the relationship of EndangeredSpecies can easily be defined.
Kudos
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I don't think you can make ENDANGERED SPECIES anything other than attribiute. Otherwise anything under PANDA would automatically become endangered which it might not be. Also, BIRTHING METHOD would have to be an attribute since some warm blooded animals lay eggs (platypus).
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Perfect..Carl and Junilu,
You are absolutely correct to mention one can suggest many solutions that revolves around the problem or the requirement.However, we need to focus on the requirement in hand.From the initial diagram (see my first post), we can probably infer that the Developer had a requirement to design Panda and some other species as Endangered.So the heart of the problem is - "How do we design the fact that Panda and some other species are endangered?"
We have identified two different inheritance heirarchies (see my immediate previous post).What we need to do now is to associate the heirarchies for addressing this requirement.
BTW, what about a solution of keeping a boolean attribute (say, isEndangered) in BEAR class or its probable superclass ANIMAL instead of associating the two heirarchies?What are the pros and cons of this solution?Does it address the problem in the first place?
Looking forward for your comments,
-- Sandeep
[This message has been edited by Desai Sandeep (edited July 07, 2001).]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112

BTW, what about a solution of keeping a boolean attribute (say, isEndangered) in BEAR class or its probable superclass ANIMAL instead of associating the two heirarchies?What are the pros and cons of this solution?Does it address the problem in the first place?

It would be ok if you didn't have to implement different behaviour depending on the state of the attribute. If you had to, it would be better to use polymorphism instead of conditionals (if/switch-statements), so I would favor something like the state-pattern.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: Perils of Inheritance
 
Threads others viewed
Operator >>>
UML Question to be answer
Brainbench Question
about substring in String.
Tips on using this discussion software
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com