Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface extends another interface but implements its methods

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!!!

like image 840
GingerHead Avatar asked Apr 19 '12 11:04

GingerHead


People also ask

Does an interface extends or implements another interface?

An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and interface never has the definition of any method.

Can a interface extend inherit another interface?

Yes, we can do it. An interface can extend multiple interfaces in Java.

CAN interface have implemented methods?

There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.

What is interface How do you extend and implement it?

By using “extends” keyword a class can inherit another class, or an interface can inherit other interfaces. By using “implements” keyword a class can implement an interface. 2. It is not compulsory that subclass that extends a superclass override all the methods in a superclass.


1 Answers

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); }; 
like image 134
AlexTheo Avatar answered Oct 12 '22 08:10

AlexTheo