overriding attributes  
Author Message
Andersen





PostPosted: 2004-12-11 16:18:00 Top

java-programmer, overriding attributes I have a class A and a class B inheriting from A.

I have a class C and a class D inheriting from C.

I would like to have a attribute ATTR of type C in A. But in B, I would
ATTR to be of type D.

What should I do?

regards,
Andersen
 
Brett Foster





PostPosted: 2004-12-11 18:26:00 Top

java-programmer >> overriding attributes Andersen wrote:
> I have a class A and a class B inheriting from A.
>
> I have a class C and a class D inheriting from C.
>
> I would like to have a attribute ATTR of type C in A. But in B, I would
> ATTR to be of type D.
>
> What should I do?

Not quite possible:

Override the get/set functions instead of the attribute such that they
throw IllegalArgumentException when the wrong class is passed in. This
doesn't sound like good OO design, though.

Brett

>
> regards,
> Andersen
 
Paul van Rossem





PostPosted: 2004-12-11 19:43:00 Top

java-programmer >> overriding attributes On 11-12-2004 09:17, Andersen wrote:
> I have a class A and a class B inheriting from A.
>
> I have a class C and a class D inheriting from C.
>
> I would like to have a attribute ATTR of type C in A. But in B, I would
> ATTR to be of type D.
>
> What should I do?
>
> regards,
> Andersen
What do you mean by "Class A inheriting from A?"

regards, Paul.
 
 
Chris Smith





PostPosted: 2004-12-11 23:44:00 Top

java-programmer >> overriding attributes Andersen <email***@***.com> wrote:
> I have a class A and a class B inheriting from A.
>
> I have a class C and a class D inheriting from C.
>
> I would like to have a attribute ATTR of type C in A. But in B, I would
> ATTR to be of type D.
>
> What should I do?

I'll make a weaker statement than Brett did: your parallel class
hierarchies *might* indicate that there's a better design out there.
Without more information, there's simply no way to tell.

As it is, you'd continue using a variable of type C in A, and then just
don't assign it anything but a D instance if it's actually a B.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
 
Yakov Fain





PostPosted: 2004-12-12 20:35:00 Top

java-programmer >> overriding attributes Read my article on this subject:

http://www.sys-con.com/story/?storyid=46344
Regards,
Yakov Fain
www.smartdataprocessing.com