• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many interfaces can a single interface extend?  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many interfaces can a single interface extend?
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, there is no upper limit, although I have never personally seen an Interface that extends more than 2, nor have I written one.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A single interface can extend any number of other interfaces, just as a class may implement any number of interfaces (while a class may only extend one other class).

It's important to remember that an interface exists in Java for one purpose and one purpose only--to serve as a contract. In other words, if I write an interface called Dog:



...it's important for me to define in javadocs the intent of that method. This should explicitly define what it means for a dog to bark in my particular application. This says nothing about the way the bark is carried out or how it's implemented, simply what it means. Does a sound come out of the speaker? Is a bark visually drawn on screen, as in a dialogue bubble? Or does it simply implement a bark counter somewhere in the application?

Whatever the case, the contract of this method should be clearly defined. Any interface that extends this interface or class that implements it is explicitly agreeing to the contract specified in the documentation of this method. I'm writing all this because improper use of interfaces is the number one thing that Java beginners get wrong, and while it is not strictly true to say that it's a bad idea to extend lots of interfaces, it usually isn't a good idea because it's rare (though not unheard of) that a single interface would want to honor all of those contracts.

sev
 
Greenhorn
Posts: 2
Java MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please give some examples..?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, saranya sampath.

What example would you like to see exactly? An interface can extend multiple other interfaces:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!