Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization of interface extending interface in java

Tags:

java

interface

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.

like image 987
OnePunchMan Avatar asked Dec 15 '22 20:12

OnePunchMan


1 Answers

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".

like image 100
Sergey Kalinichenko Avatar answered Dec 17 '22 11:12

Sergey Kalinichenko