INNER CLASSES AND TYPES

Inner Classes and Types


Inner clasess are 4 types :

1. Instance Inner classes

2. Local inner classes

3. Annanymous inner classes

4. Static inner class


1. Instance Inner classes

    Intance inner classes is a class under another class with non-static modifier. It is same as instance attribute or non –static method. Inside instance inner class we can’t define any static members. But we can able to access static and non-static members in outer class.

    Example 1:

     
                    
                 public class A 
                 {
                    int i;
                    static int j;
                    public class B
                    {
                       int k;
                       void m1()
                       {
                          System.out.println(i);
                          System.out.println(j);
                          System.out.println(k);
                       }
                    }
                 }                

    Inside inner class we can able to keep as many as constructors.
     
                    
                 public class C 
                 {
                    public class B 
                    {
                       B()
                       {}
                       B(int i)
                       {}
                    }
                 }                
    • We can’t keep any static attributes, methods and blocks in inner classes.

    • For inner classes, we can able to keep any access specified. But for outer class we can able to use two access specifiers either default or publicand not only outer class, any member of the file (i.e., class or interface or enums). We can able to use two access specifiers either default or public.

    Example 2:

     
                    
                 public class AB
                 {
                    private class B
                    {}
                    class C
                    {}
                    protected class D
                    {}
                    public class E
                    {}
                 }                

2. Static Inner classes

    Define a class inside another class with static access modifier or static keyword.

    Example 3:

     
                    
                 class A 
                 {
                    static class B
                    {
                    }
                 }                
    • Inside static inner class we can able to declare both static and non-static members. But we can able to access only static member from outer class but we can’t able to access non-static members from outer class.

    Example 4:

     
                    
                 public class outer
                 {
                    int i;
                    static int j;
                    static int m1()
                    {
                       return 10;
                    }
                    void m2()
                    {}
                    static class B
                    {
                       int k;
                       static int l;
                       void m3()
                       {
                          /* System.out.println(i); */
                          System.out.println(j);
                          System.out.println(k);
                          System.out.println(l);
                          System.out.println(m1());
                          /* m2(); */
                       }
                       static void m4()
                       {
                          /* System.out.println(i); */
                          System.out.println(j);
                          /* System.out.println(k); */
                          System.out.println(l);
                          System.out.println(m1());
                          /* m2(); */
                       }
                    }
                 }                

    Explanation:

    • Here System.out.println(i); ,m2(),is commented because from outer class we can’t able to access non- static,here i is non- static member in outer class.

    For static inner classes also we can use any access specifier and we can define as many as constructors.

    Example 5:

     
                    
                 public class AB
                 {
                    private class B
                    {}
                    class C
                    {}
                    protected class D
                    {}
                    public classE
                    {}
                 }                

3. Local Inner classes

    local inner classes is a class which is defined inside a method or any executable blocks(i.e., methods, constructor and blocks ). For local inner class we can’t use static keyword means there is no static inner class.

    Example 6:

     
                    
                 public class C 
                 {
                    public void m1()
                    {}
                    static public class B
                    {
                       B()
                       {
                          class E
                          {}
                       }
                    }
                 }                
    • Inside local inner classes we can able to declare only non static attributes but we can able to access both static and non-static members from outer class.

    Example 7:

     
                    
                 public class outerA
                 {
                    int i;
                    static int j;
                    void m1()
                    {
                       class A
                       {
                          int k;
                          /*  static int l;  */
                          void m2()
                          {
                             System.out.println(i);
                             System.out.println(j);
                             System.out.println(k);
                             /*  System.out.println(l);  */
                          }
                          /* static void m4()  */
                          /*   {  */
                          /*   }  */
                       }
                    }
                 }                

    Explanation:

    Here static int l; ,system.out.println(l); ,static void m4() is commented because inside local inner classes we can’t declare any static attributes here static keywords is only for global but not for local.
    • Local inner classes are private or specific to that method or block. We can’t access local inner classes out of that executable block.

    • For local inner classes we can’t able to use any access specifiers except default.Itis same as local variables and we can able to use final access modifier.

    • Final keyword we can use for any class type.

4. Anonymous Inner classes

    Anonymous inner classes is a class which is defined inside another executable blocks without any specific name with the help of super class name.

    Example 8:

     
                    
                 public class AN
                 {
                    void m1()
                    {
                       A a2=newA();
                       A a1=newA();
                       {
                       };
                    }
                 }                
    • Anonymous inner classes also equal to the local inner classes except defining class.

    • For anonymous inner class also we can’t able to use any access specifier except default we can able to use “final”access modifier.

    Example 9:

     
                    
                 public class outerB
                 {
                    class A
                    {
                       int i;
                       static int j;
                       void m2()
                       {
                          A a1=newA();
                          A a2=newA();
                          {
                             int k;
                             /* static int l; */
                             void m3()
                             {
                                System.out.println(i);
                                System.out.println(j);
                                System.out.println(k);
                                /*  System.out.println(l);  */      
                             }
                             /* static void m4() */
                             /*   {} */
                          };
                       }
                    }
                 }                

    Example 10:

     
                    
                 class Inm
                 {
                    Inm()
                    {
                       System.out.println("no arg");      
                    }
                    Inm(int i)
                    {
                       System.out.println("int i");      
                    }
                 }
                 class Bnm
                 {
                    public void m1()
                    {
                       System.out.println("SDJ");
                       Inm i=newInm()
                       {
                       };
                       Inm i1=newInm(10)
                       {
                       };
                       /*  Inm i2=newInm("sudha")  */
                       /*  Here compilation fails  because we are trying to create object to  */
                       /*  anonymous inner class by using String parameterized constructor.  */
                       /*  But  there is no constructor  with string inside super class.  */
                       /*  If super class won’t contain then there is no corresponding constructor  */
                       /*  inside subclass also  */
                       {
                       };
                    }
                    class Aic extends Inm
                    {
                       Aic()
                       {
                          super();            
                       }
                       Aic(int i)
                       {
                          super(i);
                       }
                    }
                    public class Ingc
                    {
                       public static void main(String[] args)
                       {
                          Inm ip=newInm();
                          Aic a1=newAic(10);
                          Bnm b1=newBnm();
                       } 
                    }
                 }                
    • Sub class contain constructors same as the super class constructors.

    • Inside every constructor it will call corresponding super class constructor by using super().

    • Generally by default we will get super() with no arg. But in case of Anonymous inner class sub class is different.

    • Each constructor contains corresponding super() with same constructor chaining.

How To Access Inner Classes:

Creating .class files for Instance/Static classes:

Syntax:

  Before Compilation:

  Compilation output:

    javapadho
    Creating .class files for Instance/Static classes

Creating Object To Static Inner Class:

  Compilation output:

    javapadho
    Creating .class files for Instance/Static classes

    Example 11:

     
                    
                 class B
                 {
                    static class C
                    /* Here compilation is success because inside one static Inner class  */
                    /* we can able to define  static and non static members also.  */
                    {
                       class D
                       { }
                       static class E
                       { }
                    }
                 }                

    Example 12:

     
                    
                 interface X
                 {
                    /* Here compilation success because even if we define non static  */
                    /* class inside interface.it will convert automatically into static. */ 
                    class Y
                    {
                   
                    }
                 }                
    • Here compilation success even if we define non static class inside interface.it will convert automatically into static.

    • Inside interface we can define one class. .class file is X$Y.class creating object for this is X.Y x1=new X.Y();

    • We can able to define a class inside any member of the file i.e,(class/interface/enum).

    Example 13:(Before Compilation)

     
                    
                 interface X
                 {
                    int i=20;
                    void m1();
                    class Y
                    {
                       public static void main(String srgs[])
                       {
                          X.Y y1=new X().new Y();
                          /*  Here compilation fails why because we can’t create object for Interface.  */
                       }
                    }
                 }                

    Example 13:(After Compilation)

     
                    
                 interface X
                 {
                    public static final int i=20;
                    public abstract void m1();
                    public static class Y
                    {
                       public static void main(String srgs[])
                       {
                          X.Y y1=new X().new Y();
                          /* Here compilation fails why because we can’t create object for Interface. */
                       }
                    }
                 }                

How To Create Object To Local Inner Class

    Example 14:

     
                    
                 class X
                 {
                    void m1()
                    {
                       int i;
                       class Y
                       {   
                       }
                       i=20;
                       System.out.println(i);
                       X x1= new X1();
                       Y y1=newY();
                    }
                 }                

      Local Inner class has scope within that the block only. It can act as local variable.

     
                    
                 class X
                 {
                    void m1()
                    {
                       class Y
                       {}
                    }
                    class Y
                    {}
                 }                

How To Create Object To Anonymous Inner Class:

    Example 15:

     
                    
                 class A
                 {
                    void m1()
                    {
                       A a1=new A();   /*   sub class to class A.  */ 
                       {
                       };              /*  here “a1” pointing to Anonymous Inner Class.  */
                    }
                 }                

      There is no separate *.class file for Anonymous Inner Class.

      By using super class name and constructor we are creating object to Anonymous Inner Class.

      Like:
      A a2 = new A(){
      }
      Here “a2” pointing to Anonymous Inner Class.

     
                    
                 class A
                 {
                    void m1()
                    {
                       new B()
                       {  };
                    }
                    void m2()
                    {
                       B b1=new B();  /*  b1 here compilation fail  */
                       {
                       };
                       b1=new B()
                       {
                       };
                    }
                 }
                 class B
                 {   }                 

    Example 16:

     
                    
                 interface Switch
                 {
                    public void on();
                    public void off();
                 }
                 class Fan
                 {
                    private boolean switchStatus;
                    public Switch getSwitch()
                    {
                       Switch s1 = new Switch();
                       {
                          public void on()
                          {
                             switchStatus = true;
                          }
                          public void off()
                          {
                             switchStatus = false;  
                          }
                        };
                       return s1;
                    }
                    public boolean getFanstatus()
                    {
                       return switchStatus; 
                    }
                 }
                 public class FanStatus
                 {
                    public static void main(String[] args) 
                    {
                       Fan usha = newFan();
                       Switch anchor = usha.getSwitch();
                       anchor.on();
                       System.out.println("is fan running :"+ usha.getFanstatus());
                       anchor.off();
                       System.out.println("is fan running:"+ usha.getFanstatus());     
                    }
                 }                

    Output:

    is fan running :true is fan running:false
    • Its supports strong Encapsulation.

    • Its avoids naming collision or supports name reusability.

Scroll to Top Button