19

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

  • Do you mean "super()"? super in java references the base object of the class. – Zack Jun 30 '09 at 18:19
  • 2
    @DB, I think it's ok to ask basic question that could be looked up on SO. See http://stackoverflow.com/questions/1003841/how-do-i-move-the-turtle-in-logo by Jeff. – Eugene Yokota Jun 30 '09 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! – Zack Jun 30 '09 at 18:26
  • 1
    Goggling "java super interface site:oracle.com" turned up nothing that looked promising. – Raedwald Jul 20 '13 at 20:17
  • 4
    I did use Google. Google brought me here. – m24p Sep 09 '14 at 20:59

6 Answers6

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

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
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.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
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

Tom
  • 43,810
  • 29
  • 138
  • 169
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.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • Actually, a Map is not a Collection. – ColinD Jun 30 '09 at 18:37
  • @Uri - Map doesn't extend Collection ;-) List does though, to support your point. – Robin Jun 30 '09 at 18:38
  • 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. – James Jun 30 '09 at 18:38
  • You guys are of course right. I was thinking of a Set while staring at code full of maps. Fixed. – Uri Jun 30 '09 at 19:18
1

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.

pob
  • 373
  • 3
  • 6
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.

Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27