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

    Default Inheritance help needed


    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;
   = name;
        public double getPrice() {
            return price;
        public String getName(){
            return name;

    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");
            for(int x=0; x < Purchase.size(); x++){
    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.

    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
    Rep Power



    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
    Waterford, Ireland
    Rep Power


    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 06:17 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    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;
    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, 10:58 AM
  2. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 11:41 AM
  3. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 03:53 PM
  4. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 09:18 PM
  5. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 07: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