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:
Code Java:
//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();
}*/
}
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.
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
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)
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
Originally Posted by
Mr.777
The OP clarifies, if you read it.
Why is the class abstract? I don't see any abstract methods.
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
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.
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
Originally Posted by
babe20042004
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?
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
Originally Posted by
Mr.777
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??? :confused: .. 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.
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
Originally Posted by
Norm
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.
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
Originally Posted by
Mr.777
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?
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
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.
Re: How can I change the values in an object instantiated from an abstract class?
Quote:
Originally Posted by
babe20042004
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.