Shaded or implemented?

public class SuperClass { public void doFoo() { //... } } public class SubClass extends SuperClass implements AnInterface {} public interface AnInterface { public abstract void doFoo(); } 

Why SubClass n't SubClass use the doFoo() method? I guess this is already in the superclass. But I cannot add @Override for the doFoo() method in SuperClass . So is it shaded? Or what is it called? And is this a good / normal practice?

+4
source share
3 answers

Why doesn't SubClass need to implement the doFoo () method?

Because it is already implemented in SuperClass .

But I cannot add @Override for the doFoo () method in SuperClass.

No, you cannot, because SuperClass does not implement AnInterface . It does not cancel any method.

So is it shaded?

No. Tenerization is something else. This applies to variables. So, if SuperClass has a varaible foo , and in SubClass you are trying to define a variable with the same name that will obscure. It has no name.

And is this a good / normal practice?

This is a common practice. I have seen this several times in many large projects.



TL; DR An example follows :

Let's say we have the interface needed to implement ModelBasedWizard (a wizard is a common thing in many desktop applications).

 public interface IModelBasedWizard { public void addWizardPage(IWizardPage page); public IStatus getWizardStatus(); public void bindModel(IModel model); } 

Say that there is already an implementation of the 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 the visual part of the Wizard, but she knows nothing 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 }); } } 
+1
source

Generally speaking, there is nothing wrong with what you have done. The subclass inherits the doFoo method from the parent class, which satisfies the requirements imposed by the interface.

If you want your code to be too clear (perhaps to help with future maintenance), one option is:

 public class SubClass extends SuperClass implements AnInterface { @Override public void doFoo() { super.doFoo(); } } 

I'm not sure there is a specific term for your original question. Perhaps a "slightly confusing inheritance"?

+1
source

So interfaces are used. When you implement an interface, all that changes in the class is that you need to have methods in your class defined by that interface.

All you have to do to work with your code is to put an empty method called doFoo() in your subclass.

0
source

Source: https://habr.com/ru/post/1488319/


All Articles