Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Inheritence and problems with static methods.

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inheritence and problems with static methods.

    I'm having some trouble getting this program to work, my assignment deals with inheritance, and I'm basically trying to call my methods in main in order to run them.... so here's what I've gotten so far.
    import javax.swing.*;
    public class Order 
    {
    	private static String customer, numb, quant, unitPrice;
    	private static int custNumber;
    	private static double orderQuant, unitCost;
    	public static double total;
     
    	public static void setCustomer()
    	{
    		customer = JOptionPane.showInputDialog(null,"Enter customer name:");		
    	}
    	public static String getCustomer()
    	{
    		return customer;
    	}
    	public static void setCustNumber()
    	{
    		numb = JOptionPane.showInputDialog(null, "Enter costumer number:");
    		custNumber = Integer.parseInt(numb);
    	}
    	public static int getCustNumber()
    	{
    		return custNumber;
    	}
    	public static void setOrderQuant()
    	{
    		quant = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
    		orderQuant = Double.parseDouble(quant);
     
    	}
    	public static double getOrderQuant()
    	{
    		return orderQuant;
    	}
    	public static void setUnitCost()
    	{
    		unitPrice = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
    		unitCost = Double.parseDouble(unitPrice);
     
    	}
    	public static double getUnitCost()
    	{
    		return unitCost;
    	}
    	public static void computePrice()
    	{
    		total = (orderQuant * unitCost);
    	}
    	public static void display()
    	{
    		JOptionPane.showMessageDialog(null, "Customer Number: " + custNumber + 
    "\nCustomer Name: " + customer + "\nQuantity Ordered: " + Math.round(orderQuant) + 
    				"\n Price Each: $" + unitCost + "\nThe cost of your order is: $" + total);
    	}	
     
    }
    public class ShippedOrder extends Order
    {
    	private final double shipHandle = 4.00;
    	public void computePrice()
    	{
    		super.total = ((getOrderQuant() * getUnitCost()) + shipHandle);
    	}
    	public double getTotal()
    	{
    		return total;
    	}
    }
    import javax.swing.*;
     
    public class UseOrder
    {
    	public static void main(String[] args)
    	{
    		JOptionPane.showMessageDialog(null, "First, enter data for a pick up order.");
    		Order.setCustNumber();
    		Order.setCustomer();
    		Order.setOrderQuant();
    		Order.setUnitCost();
    		Order.computePrice();
    		Order.display();
     
    		JOptionPane.showMessageDialog(null, "Now, enter data for a shipped order.");
    		Order.setCustNumber();
    		Order.setCustomer();
    		Order.setOrderQuant();
    		Order.setUnitCost();
    		ShippedOrder.computePrice();
    		Order.display();
    	}
    }

    basically the problem is with the computePrice() methods in both the Order, and ShippedOrder classes. computePrice() method in Order, is supposed to be overridden by the computePrice() in ShippedOrder, basically adding $4.00 to the total. However if I make the computePrice() method in Order non-static, the UseOrder class can't use it but ShippedOrder class can, and if I make it static UseOrder class can use it, but ShippedOrder can't.
    Quite a conundrum for me. I did read something about creating a new instance of the method perhaps? But dealing with inheritance I'm not quite sure how to go about doing such a thing. Thank you in advance for your help.
    Last edited by Sylis; November 3rd, 2012 at 08:52 PM. Reason: Solution solved


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    If you're learning OOP principles, one of the first rules to learn is that you should avoid static anything except in very specific situations. Here your use of static methods are preventing inheritance from working at all. I suggest that *all* of your methods be instance or non-static methods with the only exception being the main method. To call non-static methods from the main method (or any static method), you need to first create an instance of your class, and then call the methods on that instance. In your code above, you're trying to call methods directly on the class, which again you shouldn't be doing. Create an Order object or a ShippedOrder object and call the *non-static* methods from the object.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritence and problems with static methods.

    You mean to instead of creating three different classes I should just have one class with an Order and ShippedOrder method in the one class? The assignment is to create three classes I can't really get around that part.

    Unless I totally missed what you meant, which I probably did.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    Quote Originally Posted by Sylis View Post
    You mean to instead of creating three different classes I should just have one class with an Order and ShippedOrder method in the one class? The assignment is to create three classes I can't really get around that part.

    Unless I totally missed what you meant, which I probably did.
    Yep, you missed what I meant. Let me try again. You should have one class, Order, that has no static methods, no static fields, no static *anything*. You should have another class, ShippedOrder, that extends Order, that likewise contains *nothing* static. You should have a third class, UseOrder, that has a static main method, that creates an instance of ShippedOrder in main, and that calls methods on this instance.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritence and problems with static methods.

    Thank you, I figured it out by using:
    import javax.swing.*;
     
    public class UseOrder
    {
    	public static void main(String[] args)
    	{
    		Order anOrder = new Order();
    		JOptionPane.showMessageDialog(null, "First, enter data for a pick up order.");
    		anOrder.setCustNumber();
    		anOrder.setCustomer();
    		anOrder.setOrderQuant();
    		anOrder.setUnitCost();
    		anOrder.computePrice();
    		anOrder.display();
     
    		ShippedOrder aShippedOrder = new ShippedOrder();
    		JOptionPane.showMessageDialog(null, "Now, enter data for a shipped order.");
    		anOrder.setCustNumber();
    		anOrder.setCustomer();
    		anOrder.setOrderQuant();
    		anOrder.setUnitCost();
    		aShippedOrder.computePrice();
    		anOrder.display();	
    	}	
    }

    my only problem now is the overriding of the computePrice(); method, the second compute price should be calculating an extra $4.00 on top for shipping and handling, but the computePrice() method in aShippedOrder isn't overriding the total from Order.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    Again, have you changed the code in Order and ShippedOrder as I've recommended?

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritence and problems with static methods.

    Yes I have.
    import javax.swing.*;
    public class Order 
    {
    	private String customer, numb, quant, unitPrice;
    	private int custNumber;
    	private double orderQuant, unitCost;
    	public double total;
     
    	public void setCustomer()
    	{
    		customer = JOptionPane.showInputDialog(null,"Enter customer name:");		
    	}
    	public  String getCustomer()
    	{
    		return customer;
    	}
    	public  void setCustNumber()
    	{
    		numb = JOptionPane.showInputDialog(null, "Enter costumer number:");
    		custNumber = Integer.parseInt(numb);
    	}
    	public int getCustNumber()
    	{
    		return custNumber;
    	}
    	public void setOrderQuant()
    	{
    		quant = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
    		orderQuant = Double.parseDouble(quant);
     
    	}
    	public double getOrderQuant()
    	{
    		return orderQuant;
    	}
    	public void setUnitCost()
    	{
    		unitPrice = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
    		unitCost = Double.parseDouble(unitPrice);
     
    	}
    	public double getUnitCost()
    	{
    		return unitCost;
    	}
    	public void computePrice()
    	{
    		total = (orderQuant * unitCost);
    	}
    	public void display()
    	{
    		JOptionPane.showMessageDialog(null, "Customer Number: " + custNumber + "\nCustomer Name: " + customer + "\nQuantity Ordered: " + Math.round(orderQuant) + 
    				"\n Price Each: $" + unitCost + "\nThe cost of your order is: $" + total);
    	}	
     
    }
    public class ShippedOrder extends Order
    {
    	private final double shipHandle = 4.00;
    	public void computePrice()
    	{
    		total = ((getOrderQuant() * getUnitCost()) + shipHandle);
    	}
    	public double getTotal()
    	{
    		return total;
    	}
    }

    I took out every static out of the program, except main.

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    Check your UserOrder's main method again. Which object are you using in the second half of this method, the Order object or the ShippedOrder object. Check again.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritence and problems with static methods.

    Shouldn't it be the ShippedOrder method? The first set of dialog boxes set up the information, and then the computeOrder() in Order computes only quantity * price, and the computeOrder uses quantity * price + 4.00 for shipping and handling.

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    Shouldn't it be the ShippedOrder method?
    Yes it should be, but what objects are you using mostly here?:

                    // second half of main method
    		ShippedOrder aShippedOrder = new ShippedOrder();
    		JOptionPane.showMessageDialog(null, "Now, enter data for a shipped order.");
    		anOrder.setCustNumber();  // ** anOrder??? **
    		anOrder.setCustomer();  // ** anOrder??? **
    		anOrder.setOrderQuant();  // ** anOrder??? **
    		anOrder.setUnitCost();  // ** anOrder??? **
    		aShippedOrder.computePrice();
    		anOrder.display();  // ** anOrder??? **

    Also, note that your setXXX(...) methods are non-standard. Usually they accept a parameter with which you set the class field, and usually they don't themselves do any user interaction (i.e., they don't contain Scanner or JOptionPane code). Perhaps this is what your instructor wants, but again typically the setXXX(...) methods don't do this.

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritence and problems with static methods.

    Well, I use the methods to set the customer number, customer name, order quantity, and unit cost from Order, and then computePrice from a shipped order class in order to calculate the four dollars into it total. should, ShippedOrder have it's own set methods for each variable?

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    Quote Originally Posted by Sylis View Post
    Well, I use the methods to set the customer number, customer name, order quantity, and unit cost from Order, and then computePrice from a shipped order class in order to calculate the four dollars into it total. should, ShippedOrder have it's own set methods for each variable?
    ShippedOrder already has all of the non-static methods of Order. That's what inheritance is all about. Again please make the indicated method calls on the ShippedOrder object, not the Order object.

  13. #13
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritence and problems with static methods.

    You sir are a gentleman and a scholar.
    I like that you gave me the answers I was looking for without just giving it to me. Made me think, and using my head, I figured it out, and instantly inheritance makes a world more sense to me than it originally did. Thank you very much.

  14. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    You're very welcome!

  15. #15

    Default Re: Inheritence and problems with static methods.

    Wow you guys are over-shooting the mark. The solution is far more simple:

    1. Static fields and methods are not associated with objects. Thus - using them is out of the question, if you want to have sub-classes.
    2. To use a method from another class, even non-static, you create a new instance of it. You don't have to make the methods static.

    There, problem solved.

  16. #16
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Inheritence and problems with static methods.

    Quote Originally Posted by Programming_Hobbyist View Post
    Wow you guys are over-shooting the mark. The solution is far more simple:

    1. Static fields and methods are not associated with objects. Thus - using them is out of the question, if you want to have sub-classes.
    2. To use a method from another class, even non-static, you create a new instance of it. You don't have to make the methods static.

    There, problem solved.
    Aren't you being a bit presumptuous here? Please explain how my advice was "over-shooting the mark". I told him basically the same thing in my first post if you'd take the time to read the whole thread.

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. When to use static methods?
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: June 7th, 2012, 01:51 AM
  3. Static Methods Problem
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 09:09 AM
  4. Help me understand when to use static on functions/methods
    By thenk24 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 1st, 2011, 11:42 AM
  5. Problem with inheritence? arguments not applicable
    By rbk in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2011, 09:37 AM