Can any one justify the use of inner classes with example(if possible)

Recommended Answers

All 3 Replies

To be visible to just parent class.

namespace Example
{
class Parent 
{
void Method()
{
Child c = new Child();//No error
}
class Child
{
}
}
class Painless
{
void Method()
{
Child c = new Child();//error
}
}
}

Sometimes you need an instance of a class - eg as a Listener for a Swing Event,, or a Runnable for a Thread - and these are most conveniently and compactly created locally as inner classes, often anonymous inner classes.

And inner classes can provide specific functionality for their parent classes. There are relevant examples in the Sun tutorials on inner classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.