Confused about inheritance

Discussion in 'Java' started by Jacob Weber, Jun 11, 2004.

  1. Jacob Weber

    Jacob Weber Guest

    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
    Jacob Weber, Jun 11, 2004
    #1
    1. Advertising

  2. Jacob Weber wrote:

    > 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 {}
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, Jun 11, 2004
    #2
    1. Advertising

  3. Jacob Weber

    Neal Gafter Guest

    Jacob Weber wrote:
    > 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.
    Neal Gafter, Jun 11, 2004
    #3
  4. On 11 Jun 2004 08:12:48 -0700, Jacob Weber wrote:

    > 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?

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jun 11, 2004
    #4
  5. Jacob Weber

    Jacob Weber Guest

    In article <1xwpd38i1lw85.1p79a4g6kfi2m$>,
    Andrew Thompson <> wrote:

    > 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
    Jacob Weber, Jun 11, 2004
    #5
  6. Jacob Weber

    Jacob Weber Guest

    In article <>,
    Neal Gafter <> wrote:

    > 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
    Jacob Weber, Jun 11, 2004
    #6
  7. "Dario (drinking coffee in the office…)" <> scribbled the following:
    > Jacob Weber wrote:
    >> 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;}


    Surely you mean class A2 extends A?

    > class B {}
    > class B1 extends B {}
    > class B2 extends B {}


    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "C++ looks like line noise."
    - Fred L. Baube III
    Joona I Palaste, Jun 11, 2004
    #7
  8. Jacob Weber

    Neal Gafter Guest

    Dario (drinking coffee in the office…) wrote:
    > Jacob Weber wrote:
    >
    >> 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 {}


    You forgot to include a call to a method from B inside class A.
    Neal Gafter, Jun 12, 2004
    #8
  9. Joona I Palaste wrote:
    > "Dario (drinking coffee in the office…)" <> scribbled the following:
    >
    >> Jacob Weber wrote:
    >>
    >>> 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;}

    >
    > Surely you mean class A2 extends A?


    Sure. I made a typo.

    >> class B {}
    >> class B1 extends B {}
    >> class B2 extends B {}
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, Jun 14, 2004
    #9
  10. Neal Gafter wrote:

    > Dario (drinking coffee in the office…) wrote:
    >
    >> Jacob Weber wrote:
    >>
    >>> 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 {}

    >
    >
    > 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(){...;}}
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, Jun 14, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,137
    Martijn van Steenbergen
    Dec 21, 2003
  2. Beach Potato
    Replies:
    7
    Views:
    7,673
    tom_usenet
    Sep 12, 2003
  3. cppsks
    Replies:
    0
    Views:
    819
    cppsks
    Oct 27, 2004
  4. karthikbalaguru
    Replies:
    9
    Views:
    1,036
  5. System Administrator
    Replies:
    2
    Views:
    78
Loading...

Share This Page