Confused about inheritance

J

Jacob Weber

Hello. I'm having a little problem understanding one aspect of
inheritance. It's a little hard to explain, but I think it must be a
common problem.

I have two base classes, each with the same number of subclasses:
A B
/ \ / \
A1 A2 B1 B2

I need A1 to contain a field with type B1, and likewise A2 should
contain a field of type B2.

Now, since B1 and B2 share common methods (from B), I would like to be
able to call those methods from A. Even though A doesn't know the
specific type of the field, it knows it's an instance of B, right?

Is there a way to make this work?
Jacob
 
G

Guest

Jacob said:
Hello. I'm having a little problem understanding one aspect of
inheritance. It's a little hard to explain, but I think it must be a
common problem.

I have two base classes, each with the same number of subclasses:
A B
/ \ / \
A1 A2 B1 B2

I need A1 to contain a field with type B1, and likewise A2 should
contain a field of type B2.

Now, since B1 and B2 share common methods (from B), I would like to be
able to call those methods from A. Even though A doesn't know the
specific type of the field, it knows it's an instance of B, right?

Is there a way to make this work?
Jacob

class A {}
class A1 extends A {B1 b1;}
class B1 extends A {B2 b2;}
class B {}
class B1 extends B {}
class B2 extends B {}
 
N

Neal Gafter

Jacob said:
Hello. I'm having a little problem understanding one aspect of
inheritance. It's a little hard to explain, but I think it must be a
common problem.

I have two base classes, each with the same number of subclasses:
A B
/ \ / \
A1 A2 B1 B2

I need A1 to contain a field with type B1, and likewise A2 should
contain a field of type B2.

Now, since B1 and B2 share common methods (from B), I would like to be
able to call those methods from A. Even though A doesn't know the
specific type of the field, it knows it's an instance of B, right?

Is there a way to make this work?
Jacob

Yes. Put an abstract method in A that returns a B. Implement it in A1 to
return the one contained in A1, and same for A2.
 
A

Andrew Thompson

Hello. I'm having a little problem understanding one aspect of
inheritance. It's a little hard to explain, but I think it must be a
common problem.

I have two base classes, each with the same number of subclasses:
A B
/ \ / \
A1 A2 B1 B2

Please explain a more concrete example so that
the OO gurus can help you better. I suspect
your pursuing the entirely wrong strategy if
what you describe is a requirement.

I suggest you describe what you wish to
achieve as the end result. What is the
point or purpose of what you are doing?
 
J

Jacob Weber

Andrew Thompson said:
Please explain a more concrete example so that
the OO gurus can help you better. I suspect
your pursuing the entirely wrong strategy if
what you describe is a requirement.

I suggest you describe what you wish to
achieve as the end result. What is the
point or purpose of what you are doing?


Let's say we have CookHamburger and CookHotDog classes. They're related,
so they both derive from CookFood. This contains some common methods
used by both of them, such as startGrilling() and addCondiments().

Hamburger and HotDog are classes that store customers' orders for what
they want to be cooked. These will be reused in other places, so they
need to be separate from the cooking classes. Hamburger might have
methods like withCheese(), where HotDog might have withRelish(). They
also have some methods in common, like withKetchup(), so both derive
from an Order class.

In CookFood, as I mentioned, there are some methods used by both
CookHamburger and CookHotDog. These should be able to access the methods
in Order. For example, CookFood.addContiments() adds condiments that can
be put on any type of food. So it needs to look at withKetchup(), which
is defined in Order.

But CookHamburger and CookHotDog also need to add some specific
condiments, so they extend addCondiments(). For example,
CookHotDog.addCondiments() calls super.addCondiments(), and then checks
HotDog.withRelish() before adding relish.

And no, this is not really what my application does. This is a lot more
fun. :)
Jacob
 
J

Jacob Weber

Neal Gafter said:
Yes. Put an abstract method in A that returns a B. Implement it in A1 to
return the one contained in A1, and same for A2.

So simple.....this works perfectly. Thanks!

Jacob
 
N

Neal Gafter

Dario said:
class A {}
class A1 extends A {B1 b1;}
class B1 extends A {B2 b2;}
class B {}
class B1 extends B {}
class B2 extends B {}

You forgot to include a call to a method from B inside class A.
 
G

Guest

Neal said:
You forgot to include a call to a method from B inside class A.

Something like that?

class A {}
class A1 extends A {B1 b1;}
class A2 extends A {B2 b2; void aMethod(){b2.anothermethod();} }
...
class B {void anotherMethod(){...;}}
class B1 extends B {}
class B2 extends B {void anotherMethod(){...;}}
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top