Can I change class types in a setter with an object-oriented language?
Posted
by user214626
on Stack Overflow
See other posts from Stack Overflow
or by user214626
Published on 2010-03-09T03:41:38Z
Indexed on
2010/03/09
3:51 UTC
Read the original article
Hit count: 238
object-oriented-design
|language-agnostic
Hello,
Here is the problem statement : Calling a setter on the object should result in the object to change to an object of a different class, which language can support this ? Ex. I have a class called "Man" (Parent Class), and two children namely "Toddler" and "Old Man", they are its children because they override a behaviour in Man called as walk.( i.e Toddler sometimes walks using both his hands and legs kneeled down and the Old man uses a stick to support himself).
The Man class has a attribute called age, I have a setter on Man,say it is called setAge(int ageValue). I have 3 objects, 2 toddlers, 1 old-Man. (The system is up and running,i guess when we say objects it is obvious) .I will make this call, toddler.setAge(80), I expect the toddler to change to an object of type Old Man. Is this possible.Please suggest.
Thanks,
© Stack Overflow or respective owner