Why can't an interface implement another interface?

What I mean:

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

I googled and I found this :

implements means defining an implementation for interface methods. However, the interfaces do not have an implementation, therefore it is impossible.

However, an interface is a 100% abstract class, and an abstract class can implement interfaces (100% abstract class) without implementing its methods. What is the problem when it is defined as an “interface”?

More 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 
+80
java inheritance oop interface
Oct 13 '10 at 6:59
source share
6 answers

implements means implementation when an interface intended to be declared simply to provide an interface not to be implemented.

100% abstract class functionally equivalent to interface , but it can also have an implementation if you want (in this case it will not remain 100% abstract ), so from the point of view of the JVM they are different things.

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

+86
Oct 13 '10 at 6:59
source share

implements means that the behavior will be defined for abstract methods (with the exception of abstract classes), you define the implementation.

extends means that the behavior is inherited.

With interfaces, we can say that one interface must have the same behavior as another, there is not even a real implementation. Therefore, for the extends interface, it makes sense to use a different interface instead of implementing it.




Remember that even if the abstract class can define abstract methods (a reasonable path to the interface), it is still a class and still needs to be inherited (extended) and not implemented.

+20
Oct 13 '10 at 7:02
source share

Conceptually, there are two classes and domain interfaces. Inside these domains you are always expanding, only the class implements the interface, which is a kind of "border crossing". Thus, basically “extends” for interfaces reflects behavior for classes. At least I think this is logic. It seems that not everyone agrees with this logic (I find this to be a little far-fetched), and in fact there is no technical reason to have two different keywords.

+4
Oct 13 '10 at 7:03
source share

However, an interface is a 100% abstract class, and an abstract class can implement an interface (100% abstract class) without implementing its methods. What is the problem when it is defined as an “interface”?

This is just a matter of convention. The authors of the java language decided that "extends" is the best way to describe this relationship, so we all use it.

In general, although the interface is a "100% abstract class", we do not think of them that way. Usually we see interfaces as a promise to implement certain key methods, rather than a class from which we can extract. And so we tend to use different languages ​​for interfaces than for classes.

According to others, there are good reasons for choosing a "continuation" over the "guns."

+2
Dec 18 '13 at 13:06 on
source share

hope this helps you to find out a bit what I learned in oops (core java) during my collage.

Implements a notation that defines the implementation for interface methods. However, interfaces have no implementation, so this is not possible. However, an interface can extend another interface, which means that it can add additional methods and inherit its type.

here is an example below, this is my understanding and what i learned in oops ..!

 interface ParentInterface{ void myMethod(); } interface SubInterface extends ParentInterface{ void anotherMethod(); } 

and keep one in mind, one interface can expand only another interface, and if you want to define its function on some class, then only the interface implemented, for example, below

 public interface Dog { public boolean Barks(); public boolean isGoldenRetriever(); } 

Now, if the class was to implement this interface, it would look like this:

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

and if an abstract class has a certain abstract function, define and declare, and you want to define this function, or you can say that you are implementing this function, then you intend to extend this class because the abstract class can be extended. here is an example below.

 public abstract class MyAbstractClass { public abstract void abstractMethod(); } 

Here is an example of a subclass of MyAbstractClass:

 public class MySubClass extends MyAbstractClass { public void abstractMethod() { System.out.println("My method implementation"); } } 
+1
Aug 16 '14 at 8:37
source share

An interface is a class that contains an abstract method that cannot create any object. Since the interface cannot create an object and its not a clean class, it is not worth it to implement it.

-four
Sep 19 '13 at 12:43 on
source share



All Articles