Click to See Complete Forum and Search --> : Using abstract classes and polymorphism


BuezaWebDev
02-03-2005, 06:16 AM
Hey guys,

I need some clarification on the theory of polymorphism and abstract classes.

I have an abstract parent class called "Mom", and I have child classes with different sizes, "Mark", "Jaime", "Ben", and "John".

Mom has an abstract method...

abstract int getSon(int x) {

}


How would I be able to a driver class to make Mom distinguish the name of her son when she is only given the size?

This would clear up a lot of theory for me!

Kind regards,
Jaime Bueza

ray326
02-03-2005, 12:41 PM
Your heirarchy is bad. Mom and her children are people. A person knows his "size" (not sure what that means). Mom's relationship to her children is not a subclass, it's an association like "has a". So...

Person is abstract
Person has two Parents
Parent is a Person that has a collection of children
Child is an interface that defines how a Child relates to a Parent
Mom extends Parent
Mom's son's extend Person and implement Child

The problem at hand:
Mom can iterate her collection of children, asking each one his size
If the size matches then Mom can ask the child's name

BuezaWebDev
02-03-2005, 03:57 PM
Oh, I get it now!

Thanks mate!