This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner and Outer Classes  RSS feed

 
Sumit Dhamija
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took a mock test on Sun Certification Exams and found a lot many Questions on Inner classes.
Q 1) What variables can an inner class access from the Outer class which encloses it ?
Q 2) What is the basic advantage of Outer and Inner Classes ?
Q 3) A static class cannot have a reference of its Outer class (Class which encloses it); Please clarify.

 
Aruna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Let me share my understanding with you.
Inner classes are supposed to live within the teritory of the outer classes. Outside the context of outer classes, inner classes should not have a meaning.
For example:
We have a class called Flower
which has some attributes like color, name, type....
and have some methods like isBud, hasFragrance.....
Now we want to have a class called petal which is definitely a part of the Flower class.
That is, a Flower has petals.
Outside the context of flower, the class petal has no meaning.
In such a case it is ideal to use inner and outer classes to understand and implement thier relationships properly.
Also by specifiying as inner class, we are forcing others to use the Petal class only in the context of Flower class. therby we are avoiding different interpretation of the inner class.
Moreover we can put the entire class as private and therby hide it from others.
Like this, the inner class concept is handy in many places especially to hide implementations.
Please check out java documentation for exact accessibility rights.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!