• 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 class inheritance

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-When you inherit from a superclass, do you inherit the inner class?
-When you inherit from a superclass, do you inherit the super class's public static void main() method?
-During instantiation of a sub class, the super class constructor is called first. If the super class has a static inner class, then will this inner class be initialized before the before the super class state is initialized?
SJ
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Best way to check these is with some code. So that is what I wrote up for you.
Let me give you the answers to what I found, and then I would copy the code and play with it to see what you get.
1. You don't really inherit innerclasses becuase in order to access the innerclass you have to have an instance of it. But as the code below shows, if you have an instance of the sub class, you can use that instance to create an instance of the inner class.
2. Run Sub.java after you compile this and you will see that you get Parents main method, so yes you do inherit the main method.
3. After you run the code below you will see that the Parent's constrctor is called and then the Sub's, but not the inner class. Inner classes are just like other classes and constructors are not called untile you instantiate it.
Hope this helps,

Bill
 
Sajan Joseph
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Bill,
I ran your code and this is what I understood :
1. A sub class can create an object of the inner class of its superclass.
2.Super class static innerclass is not instantiated automatically. You have to explicitly create an instance of it, before calling any of its methods.
3. and public static void main() is just like any other method inherited by the subclass. But will not be very useful unless it is overridden by the subclass.
Can you tell me the practical uses of inner classes. Do we ever use them? In fact they seem too complicated. When do we use them. What is the use of static inner classes? I am not very clear on the subject. It would be a great help if you can elucidate. I have gone through TIJ. I have a vague idea. But using an inner class in the classes I write, does not come naturally to me. Nor can I think up any particular use of them.
Thanks and regards,
SJ
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sajan, inorder to understand inner classes better, why don't you read...."Getting in touch with your inner class", at the campfire stories(the link is on the Javaranch homepage). It's a wonderful story, and I understood inner classes a lot better after reading it .
Hope this helps
Rebecca
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic