Results 1 to 4 of 4
Thread: Inheritance help needed
- 06-11-2010, 04:58 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
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.
Subclasses: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; } }
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; } }So, Pizza has variables and methods which SideOrder does not. If I was to loop through an ArrayList, like here:Java Code:public class SideOrder extends Food { public SideOrder(double price, String name){ super(price, name); } }
And I wanted to call a method that exists in Pizza but not in SideOrders, how would I do it?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(); } } }
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.
- 06-11-2010, 05:09 AM #2
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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 :)
- 06-11-2010, 05:10 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 06-11-2010, 09:52 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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:
But "instanceof" is a red flag that your classes, specifically inheritance, is probably wrong.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()); }
Similar Threads
-
Inheritance
By terahawks in forum New To JavaReplies: 1Last Post: 04-23-2010, 09:58 AM -
Inheritance
By gpio in forum Advanced JavaReplies: 14Last Post: 11-20-2009, 10:41 AM -
Inheritance example
By kris4u4ever in forum New To JavaReplies: 3Last Post: 03-21-2009, 02:53 PM -
inheritance
By itaipee in forum New To JavaReplies: 6Last Post: 01-20-2009, 08:18 PM -
Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks