anil
anil

Reputation:

superinterface in java

What is super-interface is java? what is the purpose of super-interface?

Upvotes: 21

Views: 23242

Answers (6)

pob
pob

Reputation: 381

The term superinterface has slightly different uses depending on the context of (a) interfaces only, and (b) classes, as described below:

(a) given interfaces A, and B, when B extends A then A is a superinterface of B (the explanation given above by Tom.)

see jls8, 8.1.5

(b) given class X, and interface C, when X implements C, then C is a direct superinterface of X.

see jls8, 9.1.3

The second case here is not clear from any of the prior explanations. That is, in case (b) there isn't any requirement for a hierarchical chain of interfaces in order to use the term superinterface.

Upvotes: 2

Mohammadreza Khatami
Mohammadreza Khatami

Reputation: 1332

when you have 2 interfaces with some related (same) field or methods. you should use one as superInterface which the second will extend, you shouldn't duplicate it. this is because simplicity and clarity to see what is going on exactly.

Upvotes: 0

Uri
Uri

Reputation: 89729

Interfaces in Java can extend one another, so that the extending interface supports all the things provided by the parent and the things it provides itself. For example, a Set has unique operations, but also supports everything that a Collection does.

The interfaces from which you are extending are considered super-interfaces. Note that an interface can extend multiple interfaces and therefore has multiple super-interfaces.

Upvotes: 1

Tom
Tom

Reputation: 45104

Basically, when an interface extends from other interface, it is forcing the class that implements it to implement methods in both interfaces.

If an extends clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the member types, methods, and constants of each of the other named interfaces. These other named interfaces are the direct superinterfaces of the interface being declared. Any class that implements the declared interface is also considered to implement all the interfaces that this interface extends.

You can have method overriding also :)

http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html

Upvotes: 1

Michael Myers
Michael Myers

Reputation: 191875

Here's an example:

public interface A {
    void doSomething();
}

public interface B extends A {
    void doSomethingElse();
}

In this example, A is a superinterface of B. It's like being a superclass. Any class implementing B now also implements A automatically, and must provide an implementation of both doSomething() and doSomethingElse().

Upvotes: 25

Hank Gay
Hank Gay

Reputation: 71939

If you have

public interface Bar extends Foo

then Foo would be the super-interface of Bar. This declares that all instances of Bar are also instances of Foo and can be treated as such, so you could pass a Bar instance wherever you needed to pass a Foo, etc.

Upvotes: 1

Related Questions