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 11 of 11

Thread: How can I change the values in an object instantiated from an abstract class?

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How can I change the values in an object instantiated from an abstract class?

    How can I change the values in an object instantiated from a class formed from an abstract class?
    In this program I want to decrease the amount of stock from each flower count by using a function called subRoseStock() that takes in a variable called amtRosesInStock and subtracts the order value the customer gives from it. Here is the code:

    //Flower Class
    public abstract class Flower{
    	String name;
    	String color;
    	int orderAmt;
    	double price;
     
     
     
    	Flower(){
     
    		name="Unknown name";
    		color="Unknown color";
    		orderAmt=0;
    		price=0.00;
     
    	}
     
    	Flower(String name, String color, int orderAmt){
    		this.name=name;
    		this.color=color;
    		this.orderAmt=orderAmt;
    	}
     
    	public String getFlowerName(){
    		return name;
    	}
    	public double getFlowerPrice(){
    		return price;
    	}
     
    	public String getFlowerColor(){
    		return color;
    	}
    	public  int getorderAmt() {
    		return orderAmt;
    	}
     
     
     
    }
    //Rose class
     
    public class Rose extends Flower {
     
    	public Rose(){
    		name="Rose";
    		price=1.50;
    		color="Red";
    		orderAmt=0;
     
    	}
     
     
     
    	public Rose(String name, String color, int amtinstock) {
    		super(name, color, amtinstock);
     
    	}
     
     
    	public String getFlowerName() {
    		return name;
     
    	}
     
     
    	public double getFlowerPrice() {
     
    		return price;
    	}
     
     
    	public String getFlowerColor() {
     
    		return color;
    	}
     
    	public  int getorderAmt() {
    		return orderAmt;
    	}
     
     
     
    }
    //FlowerInventory
     
    public class FlowerInventory  {
    	int amtRosesInStock;
    	int amtGeraInStock;
    	int amtOrchidInStock;
     
    public int getRoseStock(){
    	return amtRosesInStock;
    }
    public int getGeraInStock(){
    	return amtGeraInStock;
    }
    public int getOchidInStock(){
    	return amtOrchidInStock;
    }
     
    public int restockRoses(){
    	amtGeraInStock=100;
    	return amtGeraInStock;
    }
    public int restockGeranium(){
    	amtRosesInStock=100;
    	return amtGeraInStock;
    }
    public int restockOrchid(){
    	amtOrchidInStock=100;
    	return amtOrchidInStock;
    }
     
    /*public void subRoseStock(int amtRosesInStock){
    	amtRosesInStock=amtRosesInStock-Rose.getOrderAmt();
     
     
     
    }*/
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,400
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Are you getting errors or is the program not doing what you want it to do?
    Can you explain.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How can I change the values in an object instantiated from an abstract class?

    How can I change the values in an object instantiated from a class formed from an abstract class?
    You can never instantiate an object of abstract class. For more details read Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

  4. #4
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Quote Originally Posted by Mr.777 View Post
    You can never instantiate an object of abstract class. For more details read Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    The OP clarifies, if you read it.

    Why is the class abstract? I don't see any abstract methods.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Why is the class abstract? I don't see any abstract methods.
    Might be the logic or forced to do so but you can still make a class abstract without having any abstract method. Well, not the best practice but still allowed.

  6. #6
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Quote Originally Posted by babe20042004 View Post
    How can I change the values in an object instantiated from a class formed from an abstract class?
    The problem is not the abstract class, it is the fact that you haven't told your method which rose you are working with. N.B if it is not a specific rose, you have to specify your method at class level. But I think there is a general problem with your set up, and maybe this is why your abstract class has no abstract methods.

    So, how do you tell a method what variables to work with?
    Last edited by 2by4; December 15th, 2011 at 07:07 AM.

  7. #7
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Quote Originally Posted by Mr.777 View Post
    Might be the logic or forced to do so but you can still make a class abstract without having any abstract method. Well, not the best practice but still allowed.
    Of course it is "allowed", if only because a compiler shouldn't reject a class just because it is unfinished. But I do think an abstract class with no abstract methods may be indicative of a problem. What is it saying.

    It is saying that objects of this type are incomplete, unless you override at least one of them??? I don't care which one??? .. or

    I intend that you to extend this class in some way (I don't care how and you don't have to)???

    I am only asking the question. Maybe the OP has some abstract methods in mind which are not showing. They may help in understanding the issues.

  8. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Quote Originally Posted by Norm View Post
    Are you getting errors or is the program not doing what you want it to do?
    Can you explain.
    Thanks for your reply. The problem is that I don't know where to go from there to get the feature that I need. I need the function subRoseStock to take the subtract the result of getOrderAmt() from amtRosesInStock.

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Quote Originally Posted by Mr.777 View Post
    You can never instantiate an object of abstract class. For more details read Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    Sorry. I was playing around with the code and accidentally took out the abstract identifier. In your opinion do you find that an abstract class is the wrong way to go about this program?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,400
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: How can I change the values in an object instantiated from an abstract class?

    I need the function subRoseStock to take the subtract the result of getOrderAmt() from amtRosesInStock
    If you are getting errors please copy and paste them here. You have commented out the method in your posted code. Did it get errors?
    If getOrderAmt() is a method in a super class, you can call it directly without a reference.

  11. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How can I change the values in an object instantiated from an abstract class?

    Quote Originally Posted by babe20042004 View Post
    Sorry. I was playing around with the code and accidentally took out the abstract identifier. In your opinion do you find that an abstract class is the wrong way to go about this program?
    Then you must read what abstract is. By looking at the current code, you don't need abstract as there is no abstract method in your class, so no need to create it as abstract unless you need to have any of the method abstract.

Similar Threads

  1. Multilevel inheritance of an abstract class
    By user2205 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 27th, 2011, 05:00 PM
  2. Trying to show change on Image based on pixel values
    By javaGurl in forum Algorithms & Recursion
    Replies: 1
    Last Post: September 20th, 2011, 03:49 PM
  3. Abstract class
    By jeskoston in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2011, 01:46 PM
  4. abstract class
    By robinglow in forum Java Theory & Questions
    Replies: 2
    Last Post: August 20th, 2010, 01:36 AM
  5. abstract class
    By robinglow in forum AWT / Java Swing
    Replies: 2
    Last Post: August 20th, 2010, 01:36 AM