• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested Classes  RSS feed

 
jyothi sudi
Greenhorn
Posts: 4
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am a beginner in java, my question is as follows

What is the need for nested classes in java?

Can anyone give a real time example or simple program how it makes any difference when the same action is performed using an external class?

Thanks in advance...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jyothi sudi wrote:What is the need for nested classes in java?

Other than the fact that for things like anonymous classes, it's "the way Java does things", I'm not sure that you can really describe it as a 'need'.

What it is though is very useful.

Can anyone give a real time example

AbstractMap.SimpleEntry, which actually implements a nested interface - also very useful.

or simple program how it makes any difference when the same action is performed using an external class?

And that's where the problem comes: Any program that uses a nested class can be written to use a top-level class; but it won't be as good.

Quite apart from the question of why you would write Map.Entry as anything but a nested interface; the fact is that nested classes are allowed to do and "see" things that normal classes aren't, which helps with information-hiding.

To me, it also just makes sense: An Entry doesn't have any meaning except with a Map, so why not define it inside the class?

HIH

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might me useful to have subtypes subordinated to a given type.

Given a class representing a database package, subclasses can represent the sets of the output parameters of the very procedures.
Even if they are public classes, they can dwell in the source file of the enclosing class.

The other day I wrote a generator (in awk) that did this job. It was comfortable to generate only one output file.


Another example is Map.Entry. It is a useful type: look it up.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jyothi sudi wrote:Hi all, I am a beginner in java, my question is as follows

What is the need for nested classes in java?

Can anyone give a real time example or simple program how it makes any difference when the same action is performed using an external class?


One big difference between a nested class and a regular class, is that nested class has access modifiers. With it, you can create private class definitions, which in turn, allows you to prevent code outside of the outer class from instantiating specific types of objects.


For example, the java.util.Collections class has private List, Map, class definitions for the collections that are returned. And the definitions are private to prevent access -- heck, since it is private, it is not even documented by javadoc.

Henry
 
jyothi sudi
Greenhorn
Posts: 4
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!