I have an abstract class
public abstract class SuperclassA{
public abstract void method(SuperclassB b);
}
And an implementation
public class SubclassA extends SuperclassA{
@override
public void method(SubclassB b){
}
}
where SubclassB extends SuperclassB
.
My NetBeans editor complains that I am not overriding method
.
What can I do to actually pass a subclass to method
?
In order to override a method of the base class, the sub-class's method must have the same signature.
If you want the overridden method to accept only instances of SubclassB
, you can test for it :
public class SubclassA extends SuperclassA
{
@Override
public void method(SuperclassB b){
if (!(b instanceof SubclassB)) {
// throw some exception
}
SubclassB sb = (SubclassB) b;
...
}
}
You can call that method as follows :
SuperclassA a = new SubclassA ();
a.method (new SubclassB ());