Why an interface can not implement another interface?

JavaOopInheritanceInterface

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

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

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

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

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");
    }
}

Solution 6 - Java

Interface is like an abstraction that is not providing any functionality. Hence It does not 'implement' but extend the other abstractions or interfaces.

Solution 7 - Java

Interface is the class that contains an abstract method that cannot create any object.Since Interface cannot create the object and its not a pure class, Its no worth implementing it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser467871View Question on Stackoverflow
Solution 1 - JavajmjView Answer on Stackoverflow
Solution 2 - JavaColin HebertView Answer on Stackoverflow
Solution 3 - JavaLandeiView Answer on Stackoverflow
Solution 4 - JavaJosiah YoderView Answer on Stackoverflow
Solution 5 - JavaXxANxXView Answer on Stackoverflow
Solution 6 - JavaSahilView Answer on Stackoverflow
Solution 7 - JavaSundeepView Answer on Stackoverflow