lundi 27 juillet 2020

Is it right to use extend and implement in the same class

interface A {void someMethod();}

class B implements A {
 void someMethod(){
  //do something
 }
}

class C extends B implements A {
 void someMethod() {
  super.someMethod();
  //do something
 }
}

I'm using the above design in one of my codes. It is working fine. My whole purpose here is to use the default implementation of class B and do something extra in class C. Is this the correct way to use the implementation? Is there any better design patter to be looked at?

Because If I define my class C as below, still everything works fine. But this neglects the whole purpose of using implementation (to force class C to implement methods of interface A).

class C extends B implements A {}

Aucun commentaire:

Enregistrer un commentaire