简体   繁体   中英

why interfaces can be implemented in abstract classes but not in other interfaces?

I know that an interface cannot implement in another interface as implementing means writing the body of the methods. This cannot be done in interfaces as none of the methods in the interface will have the body. {Upto Java 7}

But I am confused, if this is the reason why interfaces cannot implement one another then why do the interfaces can be implemented in abstract classes. Since abstract classes cannot define all the methods of the interface necessarily. So in a way an abstract class is not implementing all of the methods of the interfaces.

Interfaces cannot use implements because their purpose is to define interfaces, not provide implementation. (They can extend other interfaces, though.) The purpose of classes is to provide implementation, even if only partial.

But like almost all rules, the edges are blurry:

  • An abstract class doesn't actually have to implement anything.
  • Now that Java interfaces have default methods , in some sense they can implement an interface, just not using the keyword implements :

     interface A { void foo(); } interface B extends A { void bar(); default void foo() { System.out.println("foo"); } } public class Example implements B { public static void main(String[] args) throws Exception { new Example().foo(); } public void bar() { } } 
  • Often, people don't bother to define an interface as distinct from the class, instead using the class both to define the interface to it and provide the implementation of it. (Some purists have an issue with that, but it's very common practice.)

Fundamentally, though: Interfaces are for defining the interface, and classes are for providing the implementation.

First: "This cannot be done in interfaces as none of the methods in the interface will have the body." : It's important to note that this is a little out of date. Interfaces can have method implementations/bodies (default and/or static methods). This is possible since Java 8.

why interfaces can be implemented in abstract classes but not in other interfaces?

Your question may be strictly about the implements keyword declaration as that's the only aspect in which it makes a difference. In this sense, it's a question of design. Abstract classes are classes, interfaces are interfaces. There are differences between these two types of component, the most notable of which, in this case, is that concrete classes cannot inherit from multiple abstract classes. There are very good answers on SO about the differences between abstract classes and interfaces (such as this ).

Conceptually, though, an interface can extends another interface and then provide an implementation for each of the inherited abstract methods with default methods. One could argue that this is an implementation of the super-interface. But when it comes to the specifics of the language, only a class (abstract or not) can declare to implement an interface.

In the end, though, whether concrete methods are in an interface or in an abstract class, before they're used, an object of a concrete class will have to be created (I'm excluding functional interfaces here), so the difference doesn't matter that much.

Interfaces can extend other interfaces. As for abstract classes: they require the implementation of the interfaces methods as soon as they implement it. However the implementation doesn't have to be in the abstract class but only in the classes that extend the abstract class and are not abstract themselves.

You have to know something. Interfaces Implements Classes and Abstract Class is a Class. From Java Docs

Interface can only be implemented by classes or extended by other interfaces.

So Why Interface can not Implement another Interface?

Because at time you are Implementing to a Class you are defining how you class behave. So Implementing an Interface to another interface will break the purposes of an Interface. Implements define that you need an implementation of the methods and the Interface doesn't have implementation only Classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM