Java why interface extends interface

peter picture peter · Nov 18, 2012 · Viewed 43.1k times · Source

I am wondering under what circumstances do we extend an interface from an interface? Because, for example

interface A{
    public void method1();
}
interface B extends A{
    public void method2();
}
class C implements B{
    @Override public void method1(){}
    @Override public void method2(){}
}

Isn't it equivalent to

interface A{
    public void method1();
}
interface B{
    public void method2();     
}
class C implements A, B{
    @Override public void method1(){}
    @Override public void method2(){}
}

Are there any significant reasons behind ?

Answer

Spencer Ruport picture Spencer Ruport · Nov 18, 2012

The significant reasons depend entirely on what the interface is supposed to do.

If you have an interface Vehicle and an interface Drivable it stands to reason that all vehicles are drivable. Without interface inheritance every different kind of car class is going to require

class ChevyVolt implements Vehicle, Drivable
class FordEscort implements Vehicle, Drivable
class ToyotaPrius implements Vehicle, Drivable

and so on.

Like I said all vehicles are drivable so it's easier to just have:

class ChevyVolt implements Vehicle
class FordEscort implements Vehicle
class ToyotaPrius implements Vehicle

With Vehicle as follows:

interface Vehicle extends Drivable

And not have to think about it.