I have an interface A with two methods:
public interface A{
method One();
method Two();
}
And I have two options for interface B
:
Case 1:
public interface B{
method One();
method Two();
method Three();
}
Case 2:
public interface B extends A{
method Three();
}
Can anyone tell me which of the above two cases is better to implement and why? Which are the downsides of Case 2?
Edit: In interface B, I need both the methods of interface A. So, I thought extending would be much better. There are some classes where I need method One and Two only, there I implement interface A. And where I need method One, Two and Three, I implement interface B.
This depends on your logic. Consider these two pairs of interfaces:
interface House {
int doorCount();
int windowCount();
}
interface Automobile {
int doorCount();
int windowCount();
int wheelCount();
}
Nobody in his right mind would derive Automobile
by extending a house, because an automobile is not a house with wheels.
On the other hand, in situation like this
interface House {
int doorCount();
int windowCount();
}
int Mansion extends House {
int poolCount();
}
deriving from House
makes sense, because Mansion
is a kind of House
with "additional features".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With