What is inner class ?

A class is a user defined datatype. A class contains variables and methods. It can also contain another class. This enclosed class in known as inner class.
When you compile the class containing inner class,
class MyOuter {
class MyInner { }
}

And if you compile it,
%javac MyOuter.java
you'll end up with two class files:
MyOuter.class
MyOuter$MyInner.class
The inner class is still, in the end, a separate class, so a separate class file is generated
for it. But the inner class file isn't accessible to you in the usual way. You can't say
%java MyOuter$MyInner
Types:
-Static inner class
-non-static inner class
-local inner class
-Annymous inner class
Advantages:
-logical grouping of classes
-increased encapsulation
-more readable and maintainable code

No comments:

Post a Comment