Results 1 to 9 of 9
  1. #1
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default a question about inheritance

    im doing on a project bout inheritance and i encounter few questions.
    for example in a child class,
    Java Code:
    private int quantity1 = super.getQuantity();
    the above code suppose to get the quantity from its parent class and storing it in quantity1 right?
    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!

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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?

  3. #3
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    ya i ran it. but it doesn't return any value
    in the parent class,
    Java Code:
    public int getQuantity()
    {
    	return quantity;
    }
    the whole program have no error, thats why im wondering what is wrong with the coding.

    the quantity is suppose to be the quantity enter by user.
    Java Code:
    public void setQuantity()
    {
    	quantity = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter quantity ordered."));
    }
    i have 2 display method, where display method in parent class works and child class shows 0 and null for every info

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  5. #5
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Try returning a hard coded number to check if it's a problem with the method call or the variable retaining a value.
    what do you mean by this? you mean replace the quantity in getQuantity function with a number?and try see if there is a problem?

    do you think its better if i post my whole coding? is it alright to do that?

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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.

  7. #7
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default

    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:
    Java 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);
    }
    }
    child class
    Java 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);
    	}
    }
    main class
    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();
    		}
    	}
    }

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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:
    Java Code:
    			ShippedOrder order2 = new ShippedOrder();
    			order2.getCustomerName();
    			order2.getCustomerNum();
    			order2.getQuantity();
    			order2.getUnitPrice();
    			order2.computePrice();
    			order2.displayInfo();
    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)

  9. #9
    litojs is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

Similar Threads

  1. inheritance basic.
    By newmember in forum New To Java
    Replies: 1
    Last Post: 08-02-2008, 09:35 PM
  2. aggrigation and inheritance
    By ramakrishna.tata in forum New To Java
    Replies: 6
    Last Post: 07-08-2008, 08:11 AM
  3. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM
  4. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 AM

Posting Permissions

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