• 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
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Inner classes

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[
class Outer {
static class Inner {
public Inner()
{
}
}
}
class InheritInner extends Outer.Inner {
public static void main(String [] args) {
Outer o = new Outer();
o.new Inner(); //1
Inner base = new Inner(); // 2
InheritInner ii = new InheritInner();
}

}
]
Why can't I do something like //2 ?
Why do I need an instance of Outer class to instantiate Inner class like in //1
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shubh:
You do not have to create an outer class object to create an instance of static inner class. It is matter of name visibility:

However if InheritInner extends Outer, then all members (except private) are implicity known by simple name in InheritInner. See code:

Another variation:

You can also play with import statement to change the visibility of class or inner class...
Hope this helps
Barkat
[ September 20, 2002: Message edited by: Barkat Mardhani ]
[ September 20, 2002: Message edited by: Barkat Mardhani ]
 
Shubh Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really don't understand much I think I am confused as to what happens when an inner class in inherited. If there is a documentation please let me know. I would love to read.
Or can anyone please explain different scenarios of what happens when I extend an inner class. Also, whats up with super(). I mean why can I do o.super in the example I gave above.
Really very confused.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why can't I do something like //2 ?


Inner class doesn't exist. The name is Outer.Inner, thus the name of the constructor really is Outer.Inner


Why do I need an instance of Outer class to instantiate Inner class like in //1


This doesn't compile in JSDK 1.4
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose:

quote:
--------------------------------------------------------------------------------
Why do I need an instance of Outer class to instantiate Inner class like in //1
--------------------------------------------------------------------------------

This doesn't compile in JSDK 1.4


Jose, I am not following your point. I contend that you do not need an outer class object to create an instance of STATIC inner class. Only issue is name visibility of STATIC inner class at the point you want to create that object....
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic