Results 1 to 4 of 4
  1. #1
    LFG
    LFG is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default Inheritance help needed

    Hi,

    I've created a little dummy program below to demonstrate something about inheritance I am having trouble with.

    Superclass is Food with two subclasses - Pizza and SideOrders.

    Java Code:
    public abstract class Food {
    
        private double price;
        private String name;
        
        public Food(double price, String name) {
            this.price = price;
            this.name = name;
        }
    
        public double getPrice() {
            return price;
        }
    
        public String getName(){
            return name;
        }
    }
    Subclasses:

    Java Code:
    public class Pizza extends Food {
    
        private String description, size;
    
        public Pizza(double price, String name, String size, String description) {
            super(price, name);
            this.size = size;
            this.description = description;
        }
    
        public String getSize() {
            return size;
        }
    
        public String getDescription(){
            return description;
        }
    }
    Java Code:
    public class SideOrder extends Food {
    
        public SideOrder(double price, String name){
            super(price, name);
        }
        
    }
    So, Pizza has variables and methods which SideOrder does not. If I was to loop through an ArrayList, like here:

    Java Code:
    public class Main {
    
        public static void main(String[] args) {
            ArrayList<Food> Purchase = new ArrayList<Food>();
            Food pizza1 = new Pizza(9.99, "Chicken Supreme", "Large", "Description here");
            Food side1 = new SideOrder(2.99, "Garlic Bread");
    
            Purchase.add(pizza1);
            Purchase.add(side1);
    
            for(int x=0; x < Purchase.size(); x++){
                Purchase.get(x).getName();
            }
        }
    }
    And I wanted to call a method that exists in Pizza but not in SideOrders, how would I do it?

    It's more the general idea of how to put inheritance into action that is causing me trouble as I know how to create the classes and the benefits of it, just not how to work with them.

    Any help is appreicated.
    Thanks

    EDIT: I know the code here presents no errors as getName() is an inherited method but if I were to replace it with getSize() it would not work. I only want to call getSize() if the object is a Pizza but that's where I get lost.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Emm...

    you can simply inst Pizza object only and use it as it is because it ext Food =)

    use code like a

    Java Code:
    Pizza pizza1 = new Pizza(9.99, "Chicken Supreme", "Large", "Description here");
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    If sideOrder extends pizza it will will inherit from food also so because pizza inherits from food the chain goes all the way up, so have sideOrder inherit pizza then it will have access to getSize() from pizza and getName() from food
    Last edited by al_Marshy_1981; 06-11-2010 at 05:17 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,796
    Rep Power
    19

    Default

    This will largely come down to your actual requirements. That is, what the system as a whole is supposed to do. You're not going to find a cookie cutter solution that applies everywhere.

    So, with that in mind, your current split of classes, if size() or description() are something you need to actually use in a cycle like this, is possibly wrong. I say this because the obvious "solution" is to use:
    Java Code:
    Food food = (Food)purchase.get(x);
    // note variables should start with a lower case so they don't look like class names
    if (food instanceof Pizza) {
        Pizza pizza = (Pizza) food;
        doSomethingWithSize(pizza.getSize());
    }
    But "instanceof" is a red flag that your classes, specifically inheritance, is probably wrong.

Similar Threads

  1. Inheritance
    By terahawks in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 09:58 AM
  2. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 10:41 AM
  3. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 02:53 PM
  4. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 08:18 PM
  5. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM

Tags for this Thread

Posting Permissions

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