I'm new to Java. I've had 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 construct a new DarkRoast
object:
Beverage beverage2 = new DarkRoast();
I expect it to have desctiption equal to "Dark roast":
assertEquals("Dark roast", beverage2.getDescription());
But actually it's "Unknown beverage". I know I should implement DarkRoast constructor that sets description, but I don't know why, I don't know how it works internally. Shouldn't the subclass field overwrite superclass field?