Inner class or Nested class



A class that is declared within another class or interface is called a nested class. Similarly, an interface that is declared within another class or interface is called a nested interface. A top-level class or a top-level interface is one that is not nested.

In addition to the top-level classes and interfaces, there are four categories of nested classes and one of nested interfaces defined by the context these classes and interfaces are declared in:

·         static member classes and interfaces

·         non-static member classes

·         local classes

·         anonymous classes

The last three categories are collectively known as inner classes. They differ from non-inner classes in one important aspect: that an instance of an inner class may be associated with an instance of the enclosing class. The instance of the enclosing class is called the immediately enclosing instance. An instance of an inner class can access the members of its immediately enclosing instance by their simple name.

Static inner class:

A static member class or interface is defined as a static member in a class or an interface. Such a nested class can be instantiated like any ordinary top-level class, using its full name. No enclosing instance is required to instantiate a static member class. Note that there are no non-static member, local, or anonymous interfaces. Interfaces are always defined either at the top level or as static members.

public class OuterClass {

    static int a;

    static int b;

    static class inner

    {

        void disp(int x, int y){

            a=x;

            b=y;

            System.out.println(a+b);

        }

    }

}

class Test{

    public static void main(String as[]){

        OuterClass.inner i=new OuterClass.inner();

        i.disp(10, 20);

    }

  

}

Note: static inner class can only access static data member of its outer class

Non-static inner class:

Non-static member classes are defined as instance members of other classes, just like fields and instance methods are defined in a class. An instance of a non-static member class always has an enclosing instance associated with it.

public class OuterClass {

    static int a;

    private int b;

    class inner

    {

        void disp(int x, int y){

            a=x;

            b=y;

            System.out.println(a+b);

        }

    }

}

class Test{

    public static void main(String as[]){

        OuterClass o=new OuterClass();

        OuterClass.inner i=o.new inner();

        i.disp(10, 20);

    }

  

}

Note: a non static inner class can access of all the members include private ones of its outer class.

Local class:

Local classes can be defined in the context of a block as in a method body or a local block, just as local variables can be defined in a method body or a local block.

public class OuterClass {

    static int a;

    private static int b;

    void disp() {

      

      class inner {

        void disp() {

          

            System.out.println("hello");

        }

    }

      inner i=new inner();

      i.disp();

}

}

class Test {

    public static void main(String as[]) {

        OuterClass o = new OuterClass();

        o.disp();      

    }

}

Anonymous class:

Anonymous classes can be defined as expressions and instantiated on the fly. An instance of a local (or an anonymous) class has an enclosing instance associated with it, if the local (or anonymous) class is declared in a non-static context.

This class is inner class without any name. Each anonymous inner class either extends a class or implements an interface.

Syntax:

Case 1: anonymous inner class extends a class

new SuperClass(arg if any)

{

Members of inner class

};

Case 2: anonymous inner class implements an interface

New InterfaceName (arg if any){

Member of inner class

};

Note: only a single instance of anonymous class is created.

public class OuterClass {

int x=10;

My display()

{

    return (new My(){

       public void show()

        {

            System.out.println("Anonymous");

        }

    });

}

public static void main(String as[]){

    OuterClass o=new OuterClass();

    My m=o.display();

    m.show();

}

}

interface My{

    void show();

}

A nested class or interface cannot have the same name as any of its enclosing classes or interfaces.

Class or Interface

{

Class     // inner class

        

}

Interface or Class

{

Interface  // inner interface

}

“An Outer class has not any knowledge of members of its inner class.”

No comments:

Post a Comment