Thursday, October 28, 2021

Nested Classes

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:

  1. 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.

  2. Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.

  3. 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)
    1. Member inner class: A class created within class and outside method.
    2. Anonymous inner class: A class created for implementing an interface or extending class. The java compiler decides its name.
    3. Local inner class: A class was created within the method.
  • Static nested class : A static class created within the class.

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top