java - In Object Oriented theory, should a derived class inherit a parent object's Interface? -
i'm self taught hobbyist programmer , knowledge derived seeing compiler , doesn't like.
suppose have (in c# notation, java may have other abilities)
the class need override looks this:
public interface icandosomethingelse { void doit(); } class parent : icandosomethingelse { public void eattacos() { } void icandosomethingelse.doit(string thingtodo) { // implementation } }
so this:
class child : parent, icandosomethingelse { new public void eattacos() { } void icandosomethingelse.doit(string thingtodo) // new keyword illegal here? { // implementation } }
question
i observe new keyword illegal in interface. because explicit interface?
is there way force children implement interface, or mean need set implicit/explicit cast?
i observe new keyword illegal in interface. because explicit interface?
the error because you're implementing interface explicitly in classes.
explicit implementaton forces methods exposed when working interface directly, , not underlying implementation.
if classes this:
class parent : icandosomethingelse { ... public void doit(string thingtodo) { // implementation } } class child : parent, icandosomethingelse { ... public new void doit(string thingtodo) { // implementation } }
you need use new
keyword somehow hide parent.doit
implementation.
is there way force children implement interface, or mean need set implicit/explicit cast?
you can make parent
class abstract
, interface's methods abstract
too:
abstract class parent : icandosomethingelse { ... public abstract void doit(string thingtodo); } class child : parent, icandosomethingelse { ... public override void doit(string thingtodo) { // implementation } }
here, child
must implement doit
method.