Reputation: 21
I have an interface called IGrocery
with one method public String getName()
.
Then there is one abstract class called AbstractFood
which implements the interface and which has a variable protected String name
.
Further there is one public class Ingredient
(with one private double amount
) which must have a constructor like this:
public Ingredient(AbstractFood food, double amount){
this.amount=amount;
???
}
I don't understand how I can create an Ingredient
object, as I would have to create an AbstractFood
object before that (but the AbstractFood
class is abstract).
Upvotes: 2
Views: 178
Reputation: 15418
If you don't want first approach: then use anonymous class approach:
AbstractFood aFood = new AbstractFood()
{
// your implementation
};
new Ingredient(aFood, someDoubleVal);
Upvotes: 0
Reputation: 5802
You have a class that inhertices from AbstractFood say:
public class Peanut extends AbstractFood
{
public Peanut(){
//code
}
}
That way, you could give an instance of "Peanut" as parameter to the constructor :)
Upvotes: 3