The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:
class OuterClass {
...
class NestedClass {
...
}
}
There are three advantages of inner classes in Java. They are as
follows:
- Nested
classes represent a particular type of relationship that is it can
access all the members (data members and methods) of the outer class, including
private.
- Nested
classes are used to develop more readable and maintainable code because
it logically group classes and interfaces in one place only.
- Code Optimization: It requires less code to write.
There are two types of nested classes non-static and static nested
classes. The non-static nested classes are also known as inner classes.
- Non-static
nested class (inner class)
- Member
inner class: A class created within class and outside method.
- Anonymous
inner class: A class created for implementing an interface or extending
class. The java compiler decides its name.
- Local
inner class: A class was created within the method.
- Static
nested class : A static class created within the class.
No comments:
Post a Comment