My Superclass is:
public abstract class MarketProduct {
private String name;
public MarketProduct(String productName) {
name = productName;
}
public final String getName() {
return this.name;
}
public abstract int getCost();
public abstract boolean equals(Object o);
}
And my subclass (up until its constructor) is:
public class Egg extends MarketProduct {
private int numEggs;
private int priceEggs;
public Egg(String productName, int numRequired, int priceEggsDozen) {
super(productName);
numEggs = numRequired;
priceEggs = priceEggsDozen;
MarketProduct marketProductEgg = new Egg(productName, numEggs, priceEggs);
}
I am getting a java.lang.StackOverflowError at Egg.init(Egg.java:9). Line 9 in this case is the last line of the constructor in the subclass, i.e:
MarketProduct marketProductEgg = new Egg(productName, numEggs, priceEggs);
I understand that a stack oveflow error arises when a recursive method keeps getting called. So I assumed that may have been a problem with the "this" in the getName method of the superclass. But removing it still caused the error at runtime. I feel that their is a problem in the structure of the code in my subclass constructor, but I am not sure what exactly. I tried to create the object higher up with the original variables, but to no avail.
Any help?