Interface extends another interface but implements its methods

126,920

Solution 1

Why does it implement its methods? How can it implement its methods when an interface can't contain method body? How can it implement the methods when it extends the other interface and not implement it? What is the purpose of an interface implementing another interface?

Interface does not implement the methods of another interface but just extends them. One example where the interface extension is needed is: consider that you have a vehicle interface with two methods moveForward and moveBack but also you need to incorporate the Aircraft which is a vehicle but with some addition methods like moveUp, moveDown so in the end you have:

public interface IVehicle {
  bool moveForward(int x);
  bool moveBack(int x);
};

and airplane:

public interface IAirplane extends IVehicle {
  bool moveDown(int x);
  bool moveUp(int x);
};

Solution 2

An interface defines behavior. For example, a Vehicle interface might define the move() method.

A Car is a Vehicle, but has additional behavior. For example, the Car interface might define the startEngine() method. Since a Car is also a Vehicle, the Car interface extends the Vehicle interface, and thus defines two methods: move() (inherited) and startEngine().

The Car interface doesn't have any method implementation. If you create a class (Volkswagen) that implements Car, it will have to provide implementations for all the methods of its interface: move() and startEngine().

An interface may not implement any other interface. It can only extend it.

Solution 3

ad 1. It does not implement its methods.

ad 4. The purpose of one interface extending, not implementing another, is to build a more specific interface. For example, SortedMap is an interface that extends Map. A client not interested in the sorting aspect can code against Map and handle all the instances of for example TreeMap, which implements SortedMap. At the same time, another client interested in the sorted aspect can use those same instances through the SortedMap interface.

In your example you are repeating the methods from the superinterface. While legal, it's unnecessary and doesn't change anything in the end result. The compiled code will be exactly the same whether these methods are there or not. Whatever Eclipse's hover says is irrelevant to the basic truth that an interface does not implement anything.

Share:
126,920
GingerHead
Author by

GingerHead

Software Architect / Project Manger, Software technology interests: Java / J2ee Spring (mvc, ws, ldap, security ...) Hibernate MyBatis Web Development (HTML 5, JavaScript, CSS 3, Jquery, Thymeleaf) Android Databases: Oracle and Sql Server You can contact me anytime! in a professional way, though

Updated on May 29, 2020

Comments

  • GingerHead
    GingerHead about 4 years

    In java when an interface extends another interface:

    1. Why does it implement its methods?
    2. How can it implement its methods when an interface can't contain a method body
    3. How can it implement the methods when it extends the other interface and not implement it?
    4. What is the purpose of an interface implementing another interface?

    This has major concepts in Java!

    EDIT:

    public interface FiresDragEvents {
    
      void addDragHandler(DragHandler handler);
    
      void removeDragHandler(DragHandler handler);
    }
    
    
    public interface DragController extends FiresDragEvents {
    
      void addDragHandler(DragHandler handler);
    
      void removeDragHandler(DragHandler handler);
    
      void dragEnd();
    
      void dragMove();
    }
    

    In eclipse there is the implement sign besides the implemented methods in DragController.

    And when I mouse-hover it, it says that it implements the method!!!

  • JB Nizet
    JB Nizet about 12 years
    I have answered them. The 3 first questions don't make sense, since an interface doesn't implement another interface, and doesn't contain any method implementation. The 4th one is answered by my answer: it allows providing additional behavior/methods.
  • JB Nizet
    JB Nizet about 12 years
    I would describe it as a small bug of Eclipse. There is no point in redeclaring the methods in the DragController interface, except if you want to provide additional javadoc.
  • overexchange
    overexchange over 9 years
    So, You are basically accumulating methods into one place called IAirplane and you actually are not inheriting. Another point is, I would ask the author of IVehicle to understand, why this interface Ivehicle was not designed to already consider moveDown() and moveBack(), because interface is discovered from existing set of concrete classes(I am assuming airplane concept is available as part of his generation). On the whole, I feel, extending an interface is a consequence of existing bad design, Because we are breaking the purpose of extends in this scenario.
  • AlexTheo
    AlexTheo over 9 years
    @overexchange no interface in this case is designed before the classes. The interface describes a functionality however the classes represent the real world objects. This is a main difference between the classes and interaces. No regarding moveUp moveDown and so on. Any vehicle can move forward and back however not all of them can move up and down. Aircraft can move forward back and also up and down but different types of aircraft will do it in a different way.
  • overexchange
    overexchange over 9 years
    i did not get you when you said: interface in this case is designed before the classes. In any case of any software design, interface is discovered after concrete classes are designed.
  • AlexTheo
    AlexTheo over 9 years
    "In any case of any software design, interface is discovered after concrete classes are designed." is not true. Examples COM in c++. In order to implement a new class you have to implement an already existing interface. Java is the same story. Take any OO library and try to extend it, you will have interfaces first...
  • overexchange
    overexchange over 9 years
    extending a library is different from writing a from-scratch application
  • Adam Hughes
    Adam Hughes over 7 years
    Overexchange, are you saying the IVehicle interface should have every conceivable method (ie. move up/down/back/forward) as a preferrable design to having an IAirplane interface to handle these cases? Would concrete implementations of cars simply leave these methods unimplemented?
  • cram2208
    cram2208 over 4 years
    How about using IVehicle and IAirplane as separate interfaces on one class? As if they were a feature set for the object implementing them? Consider: interface IVehicle { public function feature_of_IVehicle(); } and interface IAirplane { public function feature_of_IAirplane(); } then class AB implements IVehicle, IAirplane { ... }.
  • Dred
    Dred about 4 years
    And how now implements all methods? Should I point both interfaces in my Class? I want to implement only IAirplane but override 4 methods
  • AlexTheo
    AlexTheo about 4 years
    You will need to realize the IAirplane but you would need to implement all the methods as the airplan is considered vehicle in this example. Otherwise you would need to think about a different hierarchy of your interfaces.