Why does SubClass don't have to implement the doFoo() method?
Because it's already implemented in SuperClass
.
But I can't add a @Override for the doFoo() method in SuperClass.
No you cannot, because SuperClass
does not implement AnInterface
. It does not override nobody's method.
So it is shadowed?
No. Shadowing is something else. It's concerns variables. So, if SuperClass
has a varaible foo
, and in SubClass
you try define a variable with the same name, that would be shadowing. This does not have a name.
And is this a good/normal practice?
This is normal practice. I've seen it multiple times in many big projects.
TLDR Example follows:
Let's say we have an interface that is needed for implementing a ModelBasedWizard
(wizard is a common thing in many desktop apps).
public interface IModelBasedWizard {
public void addWizardPage(IWizardPage page);
public IStatus getWizardStatus();
public void bindModel(IModel model);
}
Let's say that there is already an implementation of a Wizard
public class Wizard {
public void addWizardPage(IWizardPage page) {
pages.add(page);
page.createContent(this);
}
public IStatus getWizardStatus() {
List<IStatus> stati= new ArrayList<Status>();
for (IWizardPage page : pages) {
stati.add(page.getStatus());
}
return stati;
}
}
Now, this is just a visual part of the Wizard, but it does not know anything about the model. Okay, no problem:
public class ModelBasedWizard extends Wizard implements IModelBasedWizard {
//UI, pages, stati and all other functionalities are already implemented.
//I just need to bind this wizard to a model
//create some content here
@Override
public void bindModel(IModel model) {
this.model = model;
this.fieldOne.bindToModel(model);
model.addPropertyChangeListener(new PropertyChangeListener() {
//bla bla
});
}
}