Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Nested Classes or Objects as Members

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain me that which should be used when:
1. Inner Nested classes
2. Including in one class the object/s of another class as its members
Please explain with examples.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should only use an inner class when the definition of that class is ONLY related to the enclosing class. If you make it an inner class, then you never run the risk of some other programmer trying to use that class for something that it was not intended for.
Conceptually an inner class should define something that is an integral part of the enclosing class. For instance, you might have a class for Body, which has inner classes for Eye, Heart, Kidney etc. This implies that you would not have an instance of Heart without understanding it in relation to the particular body that it belongs to and you would never create a Heart without the body.
From Mother Natures point of view this makes all the sense in the world. Of course this makes it difficult to track Heart transplants.
If you include an instance of another class in a class, then it is a "has a" relationship. A person "has a" spouse, but the spouse exists outside of the relationship to the person. The spouse person also has some meaning in the world that is unrelated to the fact that it is a spouse (probably ). For instance the spouse could be an employee of a company, with or without being married.
 
Madhur Baheti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I got the idea.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic