Can an Interface Extend Multiple Interfaces in Java

Can an interface extend multiple interfaces in Java?

Yes, you can do it. An interface can extend multiple interfaces, as shown here:

interface Maininterface extends inter1, inter2, inter3 {  
// methods
}

A single class can also implement multiple interfaces. What if two interfaces have a method defining the same name and signature?

There is a tricky point:

interface A {
void test();
}

interface B {
void test();
}

class C implements A, B {

@Override
public void test() {

}

}

Then single implementation works for both :).

Read my complete post here:

http://codeinventions.blogspot.com/2014/07/can-interface-extend-multiple.html

Can a normal Class implement multiple interfaces?

A Java class can only extend one parent class. Multiple inheritance (extends) is not allowed. Interfaces are not classes, however, and a class can implement more than one interface.

The parent interfaces are declared in a comma-separated list, after the implements keyword.

In conclusion, yes, it is possible to do:

public class A implements C,D {...}

Why can't I implement multiple interfaces?

So what I don't understand is that both AnimatesPaint and AnimatesPosition already implement createAnimator.

Yes, and those implementations conflict with one another. If you could do this, your resulting class's type would need to expose two createAnimator methods that are only differentiated by return type. Java doesn't let you have overloads that are only differentiated by return type, so you can't do that. A method signature, for overloading purposes, doesn't include the return type.

Even if they had the same return type (Animator), you'd then have two overloads with exactly the same signature, which you can't do.

They'll need to be separate methods — e.g., with separate signatures that can be differentiated — if they're going to be implemented in the same class.


In a comment you've asked:

But isn't the conflict resolved by the fact that the method has already been overriden by AnimatesPaint and AnimatesPosition? This way the implementing class ScreenElement doesn't need to implement createAnimator method, so no conflict will occur.

No, because the class itself exposes those methods (or rather, would need to) as part of its signature. Basically, suppose you could create the class and you had an instance of it, s. What would s.createAnimator(300L) do? Which one should the compiler choose?

A class's public type is composed of all of its public members, including all the public members of all of the interfaces it implements. So at a type level, it's impossible for two interfaces to implement methods with the same signature.

Check if interface is extending another interface in Java using reflection

An interface can extend multiple interfaces (just like classes can implement multiple interfaces). As such, checking which interfaces are extended is simply a matter of calling aClass.getInterfaces().

For your use case it is probably simpler to just call isAssignableFrom:

if (TopWorker.class.isAssignableFrom(aClass)) {

}

Interface extends multiple interfaces in GraphQL Schema

The answer to this question when it was asked was No, it is not possible for interfaces to extend (or implement) other interfaces.

The answer to this question today is Yes, it is possible for interfaces to implement other interfaces, but that functionality is not yet implemented in any open source GraphQL servers.

The RFC allowing interfaces to implement other interfaces was merged on January 10, 2020. An implementation of this spec for graphql-js was merged on October 8, 2019, but hasn't been released (it will ultimate be released as graphql-js@15.0.0).


For some use cases, this functionality can be emulated by having types that implement multiple interfaces. For example, consider this schema:

interface Node {
id: ID!
}

# Ideally we'd like to write `interface Pet implements Node`
# but that's not possible (yet)
interface Pet {
id: ID!
name: String!
}

type Cat implements Node, Pet {
id: ID!
name: String!
prefersWetFood: Boolean!
}

Then we can actually write the query

query {
node(id: "sylviathecat") {
... on Pet {
name
}
}
}

This is a valid query as long as there exists at least one implementation of Pet that also implements Node (in fact, the Pet interface doesn't actually need to have the id: ID! field at all).



Related Topics



Leave a reply



Submit