Monday 10 February 2014

Nested Classes

Java provides a facility like, you can define a class inside other class. This feature is called Nested classes.

    Example
    class OuterClass {
        class NestedClass {

        }
    }

Nested Classes can be static or non-static. A Nested class can be private, public, protected and default. Non-static nested classes are called inner classes.

    Example
    class OuterClass {   
        static class StaticNestedClass {

        }

        class InnerClass {

        }
    }

Uses Of Nested classes
1. Improve Encapsulation : We can make the nested classes as private, so outside world these nested classes are not visible.
2. More readable code : Making small nested classes and enclosing thse with outer classes, makes the code readable


final keyword                                                 static nested classes                                                 Home

No comments:

Post a Comment