Inner classes in java

Inner classes in java



What is inner class?

Inner classes are classes defined within other classes.


Benefits of inner classes:

1. Special relationship:

An instance of inner class share with an instance of outer class, this special relationship
gives code in the inner class access the members of outer class even they are private.

2.More organizational 

 Using packages we can organize related classes. using inner classes we can provide more organizational separation.

3.Event handling

Inner classes are more useful for event handling, like button listener, text listener etc..


They are four ways you can define inner classes:

1. Inner classes

2. Method-local inner classes

3. Anonymous inner classes

4. Static nested classes

Inner classes:

This type is simple inner class, creating  class directly into another class.

Applicable modifiers for inner class is
  1. final
  2. abstract
  3. public
  4. private
  5. protected
  6. strictfp


Example:

public class OuterClass {    

   private int x=10;
       class Inner{      
              public void m1(){  

                   System.out.println("Value of outer class member :"+x);    

          }    

   }   

    public static void main(String[] args) {      
              OuterClass.Inner inner = new OuterClass().new Inner();
   //To create instance of an inner class, you must have an instance  of the outer                  class              

          inner.m1();      
       }
}Output:

Value of outer class member :10


 2.Method-Local inner classes:

Defining an inner class with in a method.

A method local inner class can instantiated only with in the method where the inner class is defined.

Inner class object cannot use the local variables of that method. it is possible only when the variable is declared as final.

Applicable modifiers for method local inner class are
  1. abstract
  2. final

Example:


public class MethodLocal {
      
       public static void m1(){
              final int x=10;
                     class class2{
                           public void m2(){
                                  System.out.println(x+"::i'm in method local inner class");
                           }
                     }
                     class2 class2=new class2();
                     class2.m2();
                    
         }
        public static void main(String[] args) {
              MethodLocal methodLocal=new MethodLocal();
              methodLocal.m1();
               
       }

}

Output:10::i'm in method local inner class

 3.Anonymous Inner class:

Anonymous class is an inner class can be declared without any name . An anonymous class has a ability to declared and initiated a class at the same time.

There are there ways you can create an anonymous inner class  

 i. Plain old anonymous inner class type-1:

In this creating anonymous inner class of the specified class type 

Example: 

 class InnerTest {

      
       public void m1(){
              System.out.println("Im parent method");
       }
       public void m2(){
              System.out.println("Im parent method2");
       }

}
public class PlainOldAnonymous{
      
       public static void main(String[] args) {

/**below code  extended  Inner test class ,m1 method is re implemented
              InnerTest plainOldAnonymous=new InnerTest(){
                     @Override
                     public void m1(){
                           System.out.println("Im child m1 method");
                     }
                    
              };
              plainOldAnonymous.m1();
              plainOldAnonymous.m2();
       }
}:


Output:
Im child m1 method
Im parent method2


 ii. Plain old anonymous inner class type-2:

In this creating anonymous inner class of the specified interface type type
Example:

interface AnonymousExample{
       public void m1();
}

public class AnonmousIntefaceType {
       public static void main(String[] args) {
              //Below line looks like creating object for interface but it's not
              //we have implemented  interface
              AnonmousIntefaceType anonmousIntefaceType=new AnonmousIntefaceType(){
                     public void m1(){
                           System.out.println("Im m1");
                     }
                    
              };
             
       }

}


 iii.  Argument-Defined Anonymous Inner Classes:

While defining arguments also you can define inner class


interface A{
       public void m1();
}


class B{
       public void test(){
       Mytest mytest=new Mytest();
       mytest.call(new A() {
              @Override
              public void m1() {
                     System.out.println(" im implemented m1");
              }
       });
       }
             
}

class Mytest{
       public void call(A a){
              System.out.println("Hai im in MYTEST");
              a.m1();
       }
}

public class ArugumentDefinedInnerClass {
      
       public static void main(String[] args) {
              B mytest=new B();
             
              mytest.test();
       }

}

Output:

Hai im in MYTESTim implemented m1


4 ) Static nested classes:

Inner classes that are declared static are called static nested classes.A static nested class is simply a class that’s a static member of outer class.

class StaticInnerClassTest {
      
       static class StaticInnerClass{
              public static void m1(){
                     System.out.println("Im in inner class method");
              }
       }

}

public class StaticTest {
       public static void main(String[] args) {
              StaticInnerClassTest.StaticInnerClass lowlevel=new StaticInnerClassTest.StaticInnerClass();
              lowlevel.m1();
             
       }
}

Output:I’m in inner class method

Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

1 comment: