I'm writing a subclass Sub in Java which extends Sandwich and testing to see which attributes and behaviors get passed to the child class and why.
If I write it this way:
public class Sub extends Sandwich{
public Sub(){}
public String flavor(){
return "Delicious vinegar";
}
}
Then in my main I write:
Sandwich x = new Sub();
x.flavor() will return "Delicious vinegar", unless flavor() is not defined in Sandwich. However, if flavor is an attribute, it uses Sandwich's flavor instead of Sub's.
public class Sub extends Sandwich{
public String taste;
public Sub(){
taste = "Delicious vinegar";
}
I did not declare taste as static, yet x.taste will return whatever Sandwich's taste variable is (even if using an accessor method) instead of Sub's taste (or an error if Sandwich doesn't define taste). Does this have to do with the way the constructor is set up? I didn't declare any method or variable static.