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...
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?
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.
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?
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.