Let's say I have a class A and class B and both the classes needs to have common methods. Let's consider below scenario.
class A{
void commonMethod1(){...};
void commonMethod2(){...};
void someStuff();
}
class B{
void commonMethod1(){...};
void commonMethod2(){...};
void someOtherStuff();
}
In the above example , both A and B classes have 2 common methods which has same functionality. Now I would like to know whether to create a new class called Utility
and include those common methods and let A and B classes extend Utility class?
Or create a interface SomeInterface
and have 2 common methods as default methods with implementation and let class A and B implement the interface? which is the better design?
I feel implementing interface is preferred than extending a class ( so class A and class B can still extend some xyz class in future if I go for interface now) . What are your thoughts?