Results 1 to 9 of 9
Thread: a question about inheritance
- 11-10-2008, 06:13 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
a question about inheritance
im doing on a project bout inheritance and i encounter few questions.
for example in a child class,
the above code suppose to get the quantity from its parent class and storing it in quantity1 right?Java Code:private int quantity1 = super.getQuantity();
or is there a better way for getting data from a parent class?
i have only been using java for couple weeks, please help n thanks!
- 11-10-2008, 06:29 PM #2
Have you run it? It looks right to me and makes sense that it would return the quantity within the parent class. This way seems fine depending on what data your trying to get. What is quantity supposed to be?
- 11-10-2008, 06:34 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
ya i ran it. but it doesn't return any value
in the parent class,
the whole program have no error, thats why im wondering what is wrong with the coding.Java Code:public int getQuantity() { return quantity; }
the quantity is suppose to be the quantity enter by user.
i have 2 display method, where display method in parent class works and child class shows 0 and null for every infoJava Code:public void setQuantity() { quantity = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter quantity ordered.")); }
- 11-10-2008, 07:34 PM #4
quantity needs to be set in the super class before you can access the data. Try returning a hard coded number to check if it's a problem with the method call or the variable retaining a value.
- 11-10-2008, 08:35 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
- 11-10-2008, 08:47 PM #6
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Yes, he means instead of "return quantity;", replace it it "return 999;"
You're free to post your code, I think people prefer posted code to none.
- 11-10-2008, 08:53 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
ok i've tried my get function in parent class, works fine.
but the child class still return nothing.
here is my code.
parent class:
child classJava Code:import javax.swing.*; public class Order { protected String customerName; protected int customerNum, quantity; protected double unitPrice, totalPrice; public String getCustomerName() { return customerName; } public void setCustomerName() { customerName = JOptionPane.showInputDialog(null,"Please enter customer name."); } public int getCustomerNum() { return customerNum; } public void setCustomerNum() { customerNum = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter customer number.")); } public int getQuantity() { return quantity; } public void setQuantity() { quantity = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter quantity ordered.")); } public double getUnitPrice() { return unitPrice; } public void setUnitPrice() { unitPrice = Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter unit price.")); } public void computePrice() { totalPrice = quantity * unitPrice; } public void displayInfo() { JOptionPane.showMessageDialog(null,"Customer Name : " + customerName + "\nCustomer Number : " + customerNum + "\nQuantity Ordered : " + quantity + "\nUnit Price : $" + unitPrice + "\nTotal Price : $" + totalPrice); } }
main classJava Code:import javax.swing.*; public class ShippedOrder extends Order { private double extraCharge = 4; private double totalPrice; public void computePrice() { totalPrice = super.getQuantity() * (super.getUnitPrice() + extraCharge); } public void displayInfo() { JOptionPane.showMessageDialog(null,"Customer Name : " + super.getCustomerName() + "\nCustomer Number : " + super.getCustomerNum() + "\nQuantity Ordered : " + super.getQuantity() + "\nUnit Price : $" + super.getUnitPrice() + "\nShipped Price : $" + extraCharge + "\nTotal Price : $" + totalPrice); } }
Java Code:import javax.swing.*; public class UseOrder { public static void main(String[] args) { int answer, num, q; String name; double p; Order order1 = new Order(); order1.setCustomerName(); order1.setCustomerNum(); order1.setQuantity(); order1.setUnitPrice(); order1.computePrice(); order1.displayInfo(); answer = JOptionPane.showConfirmDialog(null,"Do you need the shipping n handling service?"); if(answer == JOptionPane.YES_OPTION) { ShippedOrder order2 = new ShippedOrder(); order2.getCustomerName(); order2.getCustomerNum(); order2.getQuantity(); order2.getUnitPrice(); order2.computePrice(); order2.displayInfo(); } } }
- 11-10-2008, 09:14 PM #8
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Just on a side note, you don't need a tester class (unless your project specifies that you do). You can put the main method from your UseOrder class into either of your classes.
Anyways, look at what you're doing here in your UseOrder class:
You aren't using inheritance properly, as you are getting values that aren't initialized. The order1 and order2 objects are not related, other than both being instances of Orders (where order2 is a ShippedOrder). What you want to do is use the SAME object for both tasks, which uses inheritance. (However, I don't see the necessity of inheritance for this specific project)Java Code:ShippedOrder order2 = new ShippedOrder(); order2.getCustomerName(); order2.getCustomerNum(); order2.getQuantity(); order2.getUnitPrice(); order2.computePrice(); order2.displayInfo();
- 11-10-2008, 10:07 PM #9
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
inheritance basic.
By newmember in forum New To JavaReplies: 1Last Post: 08-02-2008, 09:35 PM -
aggrigation and inheritance
By ramakrishna.tata in forum New To JavaReplies: 6Last Post: 07-08-2008, 08:11 AM -
Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:08 PM -
Inheritance in GUI
By Marty in forum SWT / JFaceReplies: 2Last Post: 05-11-2007, 12:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks