1. What is inner class and when we should go for inner classes?
 

Some times we can declare a class inside another class such type of classes are called inner classes

        Example

        Class Car{

      //more code here

      Class Engine{

            //more code here

      }

     }

Without existing Car object there is no chance of existing Engine object, hence Engine class has declared inside Car class. 
 

2.How many types of inner classes are present?

There are four types of inner classes are present 


3.What is method local inner class?

inner  classes


           Example 

           class Test{

      public  void  m1(){

               class Inner {

                               public void sum(int I,int j){

                  System.out.println(i+J);

            }//sum

             }//inner

            Inner  i=new Inner();

            i.sum(10,20);

            //more code here  

           I.sum(100,303);

           //more code here 

         i.sum(102,84);

        }//m1()

   Public  static  void main(){

      New Test().m1();

}

}  
 

4.What is anonymous inner class?

            ANONYMOUS INNER CLASS THAT EXTENDS A CLASS

            Example

            Class popcorn{

                  Public void taste(){

                        System.out.println(“it is salty”);

                  }

                  //more code here

            } 

            Class Test{

                  Public static void main(String[] args)

                        {

                        Popcorn p=new Popcorn()

                        {    // here we are creating child class for popcorn

                              }

                  p.taste()// it is sweet

                  Popcorn p=new Popcorn();

                  p1.taste() //it is salty

                  }

            } 
 
 

      class Test{

               Public static void main(String[] args){

            Runnable r=new Runnable(){

                  Public void run(){

                         for(int i=0;i<10;i++){

                        System.out.printin(“child thread”);

            };

            t.start();

            for(int i=0;i<10;i++){

                  System.out.printin(“main thread”);

      }

      }

      Don’t become fool that here we are creating object of interface Runnable.Here we are actually

               creating an object of class that is  implemented Runnable interface. 

      ANONYMOUS INNER CLASS THAT DEFINES INSIDE A METHOD ARGUMENT

      Example

      Class Test{

                      Public static void main(String[] args){

                  New Thread(new Runnable()

                                                                       {

                                    Public void run(){

                                    for(int i=0;i<10;i++){

                                          System.out.printin(“child thread”);

                                    }

                                     }).start();

                              for(int i=0;i<10;i++){

                  System.out.printin(“main thread”);

                  }//main

            }//Test 
 
 

5. With out having name of class how we can create an object and utilize the functionality of Anonymous  inner class?

   By using parent class names