subclass has a variable that is subclass of same superclass

J

jstorta

I have been running this scenario in my head for a while. In the
surface it seems fine, but I keep thinking that somehow it will cause
problems.


class A {
}

class B extends A {
}

class C extends A {
B myB;
}

Class B is a A

Class C is also a A but also has a B which is in turn a A.


Is there anything wrong with having a variable in a class of a type
that inherits from the same parent that the class itself inherits from?


Thanks.
 
M

Mike Schilling

jstorta said:
I have been running this scenario in my head for a while. In the
surface it seems fine, but I keep thinking that somehow it will cause
problems.


class A {
}

class B extends A {
}

class C extends A {
B myB;
}

Class B is a A

Class C is also a A but also has a B which is in turn a A.


Is there anything wrong with having a variable in a class of a type
that inherits from the same parent that the class itself inherits from?

Not at all. Consider that A might be "java.lang.Object".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top