I am new to Java. I had the following classes:
public abstract class Beverage {
String description = "Unknown beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}
and
public class DarkRoast extends Beverage {
String description = "Dark roast";
@Override
public double cost() {
return 0.99;
}
}
When I create a new object DarkRoast:
Beverage beverage2 = new DarkRoast();
I expect it to have a description equal to "dark hot":
assertEquals("Dark roast", beverage2.getDescription());
But actually it is an "Unknown Drink". I know that I must implement the DarkRoast constructor, which sets the description, but I don’t know why, I don’t know how it works inside. Should the subclass field overwrite the superclass field?
source
share