• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Classes

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a little confusion in inner classes.


In the above line in bold,i am using just a reference variable of class videotape to access its inner class recorder.Are we not supposed to first create an instance of videotape and then only create an instance of it's inner class?
Please clear my doubt.
Thanks...
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhanshu Mishra wrote:Are we not supposed to first create an instance of videotape and then only create an instance of it's inner class?


Yes. But that's what you're doing the line before.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mathew,
but please pardon my ignorance.Actually by instance i was confused that i have to make a real object of the class,but in the code i only have a reference of the superclass
and an object of subclass.Will the creation of inner class work even then?Without having actual object of enclosing class?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's fine. A VHS is-a VideoTape, so it inherits the ability to contain instances of the inner class.

Think about it - on line 27 the compiler doesn't know that vt is referencing a VHS object. All it knows is that it's referencing a VideoTape object. It has to allow creating the inner class for all possible objects referenced by vt, or for none of them.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that means that although we have an object of a sub class,but even that subclass's object has the ability to create object of inner class of its superclass?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. This is one example of the Liskov Substitution Principle in action - whenever an object of a class is needed, an instance of any of its subclasses can be used.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Mathew
reply
    Bookmark Topic Watch Topic
  • New Topic