Results 1 to 9 of 9
  1. #1
    andy_d is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Confused with inheritance handling

    Hi. This is an example of what I'm trying understand when it comes to inheritance

    Diner Super class

    private String item
    Diner ( String item )

    Sandwich subclass

    Sandwich ( String item, price )

    Toppings subclass of sandwich

    Toppings ( String sandwich, price, String topping, price )


    So it's clear, hierarchy is Diner > Sandwich > Toppings

    If I use the Toppings class to add the purchased item, how do I account for the two names?

    For example, if I set up the object like this....

    Toppings ( "Burger", 5, "Bacon", 1)

    It then uses the constructor...

    Toppings ( String sandwich, sandwichPrice, String topping, toppingPrice ) {
    super ( sandwich, sandwichPrice );
    this.topping = topping;
    this.price = toppingPrice;
    }

    Is that the right way of handling the arguments for the constructor? It seems like the topping String should be link back to the Diner class since it is an item still sold. I can't classify both sandwich and topping as super though. Would it be better to...

    Toppings ( String sandwich, sandwichPrice, String topping, toppingPrice ) {
    super ( topping, toppingPrice );
    this.item = sandwich;
    this.price = sandwichPrice;
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Confused with inheritance handling

    I don't see Toppings as extending from Dinner. Its more of a property of a Sandwich.
    A HamSandwich could extend Sandwich.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Confused with inheritance handling

    Quote Originally Posted by Norm View Post
    A HamSandwich could extend Sandwich.
    Oh yeah? Wouldn't a ham sandwich be a regular Sandwich, but one of its toppings (a collection property) would contain ham?

    inheritance VS composition: a fun discussion point :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    dd09wrx is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Confused with inheritance handling

    I think this is a bad example

    think of a car which has behaviors or methods such as reversing, going forward, turning left and turning right. this is the superclass

    now lets say we have to specific cars such as Audi, BMW and Mercedes which extends Car. when the extend car, they inherit the methods from the car super class. If you needed to change or extend the car behavior then all you would need to do is change the car class in one place rather than all 3 sub classes.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Confused with inheritance handling

    Quote Originally Posted by dd09wrx View Post
    I think this is a bad example
    agreed.

    think of a car which has behaviors or methods such as reversing, going forward, turning left and turning right. this is the superclass

    now lets say we have to specific cars such as Audi, BMW and Mercedes which extends Car. when the extend car, they inherit the methods from the car super class. If you needed to change or extend the car behavior then all you would need to do is change the car class in one place rather than all 3 sub classes.
    But this is no better. You're just listing brands of cars and that's the value of a property of a car, not a subclass of its own.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confused with inheritance handling

    Quote Originally Posted by gimbal2 View Post
    But this is no better. You're just listing brands of cars and that's the value of a property of a car, not a subclass of its own.
    A BMW, Audi or whatever passes the Liskov Substitution Principle so they can be sub classes of the Car class ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Confused with inheritance handling

    Anything is possible. But that doesn't mean that you SHOULD do it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confused with inheritance handling

    Quote Originally Posted by gimbal2 View Post
    Anything is possible. But that doesn't mean that you SHOULD do it.
    True, and notice the emphasis on the verb 'can' in my previous reply. Sometimes I personally dislike extension of classes; they break encapsulation w.r.t. the parent class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Confused with inheritance handling

    Quote Originally Posted by JosAH View Post
    True, and notice the emphasis on the verb 'can' in my previous reply. Sometimes I personally dislike extension of classes; they break encapsulation w.r.t. the parent class.
    Oh I did catch that, I just failed to properly acknowledge it :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Something I am very confused by:
    By MW130 in forum New To Java
    Replies: 3
    Last Post: 02-05-2013, 03:24 AM
  2. Replies: 0
    Last Post: 01-17-2013, 01:12 AM
  3. Confused in Exception Handling Assignment!!
    By rugger06 in forum New To Java
    Replies: 5
    Last Post: 04-10-2011, 02:00 PM
  4. Bit confused with inheritance
    By blug in forum New To Java
    Replies: 3
    Last Post: 11-22-2010, 01:44 PM
  5. Very confused Plz help!!
    By ratb0y in forum NetBeans
    Replies: 0
    Last Post: 02-14-2009, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •