• 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 Classing vs. Subclassing

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to have my test on Monday and have been reviewing all week. I'm a little confused on the benefit of using things like Inner classes and Local Classes. I understand the concept of subclassing, for example, parent class pet has an owner, subclass cat has what parent class pet does and then some of its own unique members, etc. As the RHE book and some others give these type of examples, such as Outer Class has Inner Class, etc. I don't understand what benefit it offers. Why wouldn't someone just sublass a parent class instead of doing nested classes? I am feeling a little unconfident as I feel I may not retain what I have learned due to not understanding the reasons to use nested classes vs. subclassing.
Can someone please offer an example that will help clarify this for me?
Thank you.
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know the difference between "has a" and "is a"?
That is the difference between subclass and inner class.
Say we have class Children extends class Parent,
so Children class "is a" Parent class, and Children class
will inherit all feature of Parent, and may be have some more.
Also say, we have class Inner which is inner class of Outer
class, then we say Outer class "has a" Inner class. The object of
Inner class may be as a member of Outer class, but there is
no link of their feature.
"has a" and "is a" are very basic concepts of object-orient, must
understand them before take exam.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
Inner classes allow you identify classes that are tightly coupled. For example, if you had a Person class and wanted to use a BirthCertificate class. Logically, if there is no Person, there would be no BirthCertificate so it makes sense to define the BirthCertificate class within the Person class. If it is created as a subclass the close relationship between the two would not be as obvious.
Local Inner classes (and I'm not 100% sure on this) are advantageous when there's an existing class that almost does everything you need. The change you want to make is minor, not something you are likely to need again; rather than write a whole new class, you create a local inner class adding the functionality you require.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes can directly access the private members of the outer class. Sub-classes can not.
 
David Basile
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all! That helps a lot. I understand the "is a" and "has a" but was forgetting to put Inner Classes in the concept of class members for "has a". I also learned that is advantageous to have the inner class in the outer class so that when they are saved they are saved in the same file, which makes good sense.
 
reply
    Bookmark Topic Watch Topic
  • New Topic