Why an interface can not implement another interface?

111,098

Solution 1

implements means implementation, when interface is meant to declare just to provide interface not for implementation.

A 100% abstract class is functionally equivalent to an interface but it can also have implementation if you wish (in this case it won't remain 100% abstract), so from the JVM's perspective they are different things.

Also the member variable in a 100% abstract class can have any access qualifier, where in an interface they are implicitly public static final.

Solution 2

implements means a behaviour will be defined for abstract methods (except for abstract classes obviously), you define the implementation.

extends means that a behaviour is inherited.

With interfaces it is possible to say that one interface should have that the same behaviour as another, there is not even an actual implementation. That's why it makes more sense for an interface to extends another interface instead of implementing it.


On a side note, remember that even if an abstract class can define abstract methods (the sane way an interface does), it is still a class and still has to be inherited (extended) and not implemented.

Solution 3

Conceptually there are the two "domains" classes and interfaces. Inside these domains you are always extending, only a class implements an interface, which is kind of "crossing the border". So basically "extends" for interfaces mirrors the behavior for classes. At least I think this is the logic behind. It seems than not everybody agrees with this kind of logic (I find it a little bit contrived myself), and in fact there is no technical reason to have two different keywords at all.

Solution 4

However, interface is 100% abstract class and abstract class can implements interface(100% abstract class) without implement its methods. What is the problem when it is defining as "interface" ?

This is simply a matter of convention. The writers of the java language decided that "extends" is the best way to describe this relationship, so that's what we all use.

In general, even though an interface is "a 100% abstract class," we don't think about them that way. We usually think about interfaces as a promise to implement certain key methods rather than a class to derive from. And so we tend to use different language for interfaces than for classes.

As others state, there are good reasons for choosing "extends" over "implements."

Solution 5

Hope this will help you a little what I have learned in oops (core java) during my college.

Implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible. An interface can however extend another interface, which means it can add more methods and inherit its type.

Here is an example below, this is my understanding and what I have learnt in oops.

interface ParentInterface{  
        void myMethod();  
}  

interface SubInterface extends ParentInterface{  
        void anotherMethod();  
}  

and keep one thing in a mind one interface can only extend another interface and if you want to define it's function on some class then only a interface in implemented eg below

public interface Dog
{
    public boolean Barks();

    public boolean isGoldenRetriever();
}

Now, if a class were to implement this interface, this is what it would look like:

public class SomeClass implements Dog
{
    public boolean Barks{
    // method definition here

    }

    public boolean isGoldenRetriever{
    // method definition here
    }
}

and if a abstract class has some abstract function define and declare and you want to define those function or you can say implement those function then you suppose to extends that class because abstract class can only be extended. here is example below.

public abstract class MyAbstractClass {

    public abstract void abstractMethod();
}

Here is an example subclass of MyAbstractClass:

public class MySubClass extends MyAbstractClass {

    public void abstractMethod() {
        System.out.println("My method implementation");
    }
}
Share:
111,098

Related videos on Youtube

nbro
Author by

nbro

don't believe the hype

Updated on July 08, 2022

Comments

  • nbro
    nbro almost 2 years

    What I mean is:

    interface B {...}
    
    interface A extends B {...} // allowed  
    
    interface A implements B {...} // not allowed
    

    I googled it and I found this:

    implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible.

    However, interface is an 100% abstract class, and an abstract class can implement interfaces (100% abstract class) without implement its methods. What is the problem when it is defining as "interface" ?

    In details,

    interface A {
        void methodA();
    }
    
    abstract class B implements A {} // we may not implement methodA() but allowed
    
    class C extends B {
       void methodA(){}
    } 
    
    interface B implements A {} // not allowed. 
    //however, interface B = %100 abstract class B
    
  • supercat
    supercat almost 11 years
    If "Y extends X" and is not sealed, then it's possible to have another type "Z" which extends "Y". That will be true whether X is an interface or a class. If "W implements X", however, then it is not possible to have "V implements W". The fact that "extends" can be "chained" and "implements" cannot seems like a good reason for having different keywords.
  • Little Santi
    Little Santi over 7 years
    Yessir. It's a matter of convention. Many people try to justify logically Sun's original Java language constraints, when it's just a personal point of view. Should the compiler have addmitted "implements" interfaces, I guess the same people would have justified it too. :-)
  • forresthopkinsa
    forresthopkinsa over 7 years
    As of Java 8, Interfaces can have default methods, making them much more similar to Abstract Classes in that respect.
  • Tao Zhang
    Tao Zhang about 7 years
    Thank you for the last sentence!