Inner class in Java

Nested Inner Class

  • A class define inside a class that is called inner class.
  • //outer class or top level class
    class A{
    
        //inner class
        class B{
    
        }
    
    }
    

  • Inner class is used to organized the a class code.
  • class A{
    
        class Logic{
            //business logic here..
        }
    
        class View{
            //design here..
        }
    
    }
    

  • An inner class also have a class that is called nested inner class.
  • //outer class
    class A{
    
        //inner class
        class B{
    
            //nested inner class
            class C{
    
            }
    
        }
    
    }
    

  • Inner class can also remove the confliction between the classes.
  • class A{
    
        class B{
            class C{
    
            }
        }
    
        class D{
            class C{
    
            }
        }
    
    }    
    

  • Inner class can also make private, protected, default, public, static, final, abstract etc.
  • class A{
    
        private class B{
    
        }
    
        protected class C{
    
        }
    
        public class D{
    
        }
    
        final class E{
    
        }
    
        abstract class F{
    
        }
    
    }    
    

  • Inner class can also access private property of outer class.
  • class A{
    
        private int i=10;
    
        class B{
    
            void show(){
                System.out.println(i);
            }
    
        }
    
        public static void main(String [] h){
            //create outer class object
            A a=new A();
    
            //create inner class object through outer reference
            B b=a.new B();
            b.show();
        }
    }    
    
    /*Note : Inner class B is non static so firstly we
    need to create an instance ot object of Outer class
    then through the outer reference create an inner class
    object to access inner class property other wise there
    is compile time error.*/
    
    Output is : 10
    

  • An inner class private data also can be access from another inner class within the same outer class through the object.
  • class A{
        
        class B{
            
            private int i=10;
    
        }
        
        class C{
            
            void show(){
    
                System.out.println(new B().i);
            
            }
    
        }
        public static void main(String [] h){
            
            A a=new A();
    
            C c=a.new C();
    
            c.show();
    
            //OR we can also call like below :
    
            new A().new C().show();
        }
    }    
    Output is : 10
    

  • A class or interface can be inner class and an interface also can have an interface or class as an inner class.
  • class A{
    
        interface B{
    
        }
    
        class G{
    
        }
    
    }   
    
    OR
    
    interface C{
    
        class F{
    
        }
    
        interface D{
    
        }
    
    }
    

  • The member interface can only be defined inside a top-level class or interface or in a static inner class.
  • class A{
    
        interface T{ // that is possible
    
        }
    
        class B{
    
            interface C{ // there is compile time error
    
            }
        }
    
        void show(){
    
            interface G{ // there is compile time error
    
            }
        }
    }
    

  • In case of inner class compiler create number of .class depends on number of class like as :
  •     class A{
            
            class B{
    
            }
    
            static class C{
    
            }
    
            void show(){
    
                class F{
    
                }
    
            }
        }
    
        //When you compile then compiler create four .class like
        //A.class, A$B.class, A$C.class and A$1F.class.
    
    

    There are 4 types of inner class :

    • Non-Static Inner class
    • Static Inner class
    • Local Inner class
    • Annonymous Inner class

    1. Non-Static Inner class :

  • A class define inside a class without using static keyword that is called Non-Static Inner class.
  •     class A{
    
            //non-static inner class
            class B{
    
            }
    
        }
    

  • Non-static inner class can be initialized by outer reference to access the property.
  • class A{
    
        void say(){
            System.out.println("Hey i am outer class method");
        }
    
    
        class B{
            void show(){
                System.out.println("Hey i am non-static inner class method");
            }
        }
    
        public static void main(String [] h){
            A a=new A();
            a.say();
    
            B b=a.new B();
            b.show();
        }
    
    }
    
    Output is : Hey i am outer class method
    Output is : Hey i am non-static inner class method
    

  • Using this keyword we can remove the confliction between variables define inside nested inner class.
  • class A{
        int i=1;
    
        class B{
            int i=2;
    
            class C{
                int i=3;
    
                class D{
                    int i=4;
    
                    void show(){
                        int i=5;
                        System.out.println(i);        // 5
                        System.out.println(this.i);   // 4
                        System.out.println(C.this.i); // 3
                        System.out.println(B.this.i); // 2
                        System.out.println(A.this.i); // 1
                    }
    
                }
            }
        }
    }    
    

  • A non-static inner class can never have static property except of constant variable.
  • class A{
    
        class B{
    
            final static int i=10; // only this can applicable
    
            static void show(){} // not applicable
    
            static int k=100; // not applicable
    
        }
    
    }    
    

    2. Static Inner class

  • A class define inside a class with static keyword that is called Static Inner class.
  • class A{
    
        //static inner class
        static class B{
    
        }
    }    
    

  • A static inner class can be access through outer class name.
  • class A{
    
        static class B{
    
            static void show(){
                System.out.println("Hey i am static inner class");
            }
    
        }
    
        public static void main(String [] h){
            A.B.show();
        }
    }    
    

  • A static inner class can also have a non static property.
  • class A{
    
        static class B{
    
            int i=100;
    
            void show(){
                System.out.println(i);
            }
        }
    
        public static void main(){
            A.B a=new A.B();
            a.show();
        }
    }    
    
    Output is : 100
    

  • A static inner class also can have a main method.
  • class A{
    
        static class B{
    
            public static void main(String [] h){
                System.out.println("Hello static inner class");
            }
    
        }
    
    }
    
    //compile by  
    javac A.java
    
    //run by
    java A$B
    
    Output is : Hello static inner class
    

    3. Local Inner class

  • Local inner class can be define inside method, constructor or block.
  • class A{
    
        void show(){
    
            //local inner class
            class B{
    
            }
    
        }
    }
    

  • Local inner class can never make private, static, protected or public.
  • class A{
        
        void show(){
        
            //local inner class
            private class B{ // there is compile time error.
        
            }
        
            protected class B{ // there is compile time error.
        
            }
    
            public class B{ // there is compile time error.
        
            }
        }
    }
    

  • Local inner class can be access within in the method body, constructor body or block body.
  • class A{
        
        void show(){
        
            //local inner class
            class B{ 
        
                void goo(){
                    System.out.println("Hey i am local inner class");
                }
    
            }
            B b=new B();
            b.goo();
    
        }
    
        public static void main(String [] h){
            A a=new A();
            a.show();
        }
    }
    
    Output is : Hey i am local inner class
    

    4. Annonymous Inner class

  • Annonymous inner class is a class that has no name.
  • Annonymous inner class is a kind of short hand code of local inner class.
  • Using Annonymous inner class we can create object of an Interface, Abstract class and normal class also.
  • Annonymous inner class generated at runtime.
  • Annonymous inner class we can pass as an argument.
  • Annonymous inner class .class is outerclass$1.class.
  • For Example :

    A simple example using local inner class :

    interface B{
    
        void add(int i,int j);
    
    }    
    class A{
    
        void show(int i,int j){
    
            class D implements B{
    
                public void add(int i,int j){
                    System.out.println(i+j);
                }
            }
            D d=new D();
            d.add(i,j);
        }
    
        public static void main(String [] h){
            new A().show(2,2);
        }
    }
    
    Output is : 4
    

    A simple example using annonymous inner class :

    interface B{
    
        void add(int i,int j);
        
    }
    
    class A{
    
        public static void main(String [] h){
            //this is the syntax of annonymous inner class
            B b=new B(){
    
                public void add(int i,int j){
                    System.out.println(i+j);
                }
    
            };
            
            b.add(2,2);
    
        }
    
    }
    
        //Annonymous class is generated at runtime when object is created.
        //Annonymous .class name is A$1.class
    
    Output is : 4
    
    

    A simple example using lambda expression :

    interface B{
    
        void add(int i,int j);
        
    }
    
    class A{
    
        public static void main(String [] h){
            B b=(i,j)->System.out.println(i+j); 
            b.add(2,2);
    
        }
    
    }
    
    Output is : 4
    
    

    No comments: