• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Nested Interface - Real-World Example?

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ... When attempting a test in http://www.danchisholm.net/july21/mybook/chapter10/exam1.html came across NESTED INTERFACES ... Below is the example as to how can nested interfaces be coded and compiled without errors ...

===
class Interface1
{
//nested interface
interface i
{
public void m1();
}

class hehe implements i
{
public void m1() { }
};
};

//interface i not visible outside the class Interface1
// needs Interface1.i to be mentioned to compile
class Try1 implements Interface1.i
{
public void m1() { }
};

interface i1
{
public void m2();
//nested interface
interface i2
{
public void m3();
}
}

//to implement inner interface i1.i2 has to mentioned
class Try2 implements i1,i1.i2
{
public void m2() { }
public void m3() { }

};
===

But I am not able to figure out a real world example as to where will it be useful. Can anyone throw light on the same. The reason why I posted this query in SCJP forum is that, ppl should know that nested interfaces are possible and should not be bowled out in exam. Any help would be appreciated. Thanks!

(Real Time in title changed to Real World)
[ October 26, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an article on their usefulness/uselessness

http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index2.html

And Flanagan said:


"Nested top-level classes and interfaces are typically used as a convenient way to group related classes."


More here:
http://www.developer.com/java/other/article.php/3358491
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at java.util.Map.Entry for a real world example.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can say that, having an interface within a class, sounds a bit logical as given in the link suggested by Louie. The example of the Inventry class containing the interface InventryItem was good, allowing the programmer to implement Inventry for his own InventryItem.

To Summarize, Interface within an Interface AND Class within an interface are allowed but NOT a GOOD PRACTICE.

And Ilja's Map.Entry was a real place where I saw it implemented too.

Thanks Louie and Ilja, for your valuable inputs !
reply
    Bookmark Topic Watch Topic
  • New Topic