21

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

asked Jun 30, 2009 at 18:15
6
  • Do you mean "super()"? super in java references the base object of the class. Commented Jun 30, 2009 at 18:19
  • 2
    @DB, I think it's ok to ask basic question that could be looked up on SO. See stackoverflow.com/questions/1003841/… by Jeff. Commented Jun 30, 2009 at 18:23
  • Thank god I didn't post an answer because I would've been wrong, lol. I was thinking super-classes. But I learnt something new -- super-interfaces! Commented Jun 30, 2009 at 18:26
  • 1
    Goggling "java super interface site:oracle.com" turned up nothing that looked promising. Commented Jul 20, 2013 at 20:17
  • 4
    I did use Google. Google brought me here. Commented Sep 9, 2014 at 20:59

6 Answers 6

25

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().

answered Jun 30, 2009 at 18:18
Sign up to request clarification or add additional context in comments.

1 Comment

Note: Eclipse also says that X & Y are a "superinterface" for "interface Z extends X,Y"
2

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.

answered Mar 10, 2017 at 21:32

Comments

1

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.

answered Jun 30, 2009 at 18:18

Comments

1

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

answered Jun 30, 2009 at 18:19

Comments

1

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.

answered Jun 30, 2009 at 18:19

4 Comments

Actually, a Map is not a Collection.
@Uri - Map doesn't extend Collection ;-) List does though, to support your point.
Actually, Map is explicitly not a Collection since its operations are defined on pairs of objects rather than a single object. The map methods are very clear whether they apply to the keys or the values: using as a Collection would be very ambiguous.
You guys are of course right. I was thinking of a Set while staring at code full of maps. Fixed.
0

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.

answered Apr 23, 2016 at 16:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.