15:Inner classes



Innnerclass means a class inside the other class.
They are 4 types of inner classes.
1.   Member innerclass
2.   Static innerclass
3.   Local innerclass
4.   Anonymous innerclass
Member innerclass:-
Member innerclass act as a non-static member of a class. So, all the rules applicable to non-static members of a class are also applicable incase of member inner class.
We can access inner class functions from outer class functions by creating an object of inner class.
Output:-

funOut of outer class
funIn of inner class
funIn of inner class
class Outer{
          int x;

static int y;
          class Inner         {
                   int a=10;
                   void funIn()                   {
                             System.out.println("funIn of inner class");
                   }        }
          void funOut()      {
                   System.out.println("funOut of outer class");
                   Inner in=new Inner();
                   in.funIn();    }
}
class Test{
          public static void main(String[] args) {
                   Outer out=new Outer();
                   out.funOut();
                   Outer.Inner oin=new Outer().new Inner();
                   oin.funIn();
          }   }
Static innerclass:-
static, private, protected keywords are access specifiers should not be used to declare a class.
We can declare innerclasses using static, private, protected also.
Static inner class will be treated as a static member of outer class. Even though static inner class contains non-static contents those will be treated as static contents of outer class only. Because, static inner class is a static block.
Program:-
class sout {
Output:-

10
funin of sinner

int x;
          static int y;
          static void funout(){
                   sout s=new sout();
                   s.x=s.x+10;
                   System.out.println(s.x);
          }
          static class sinner{
                   int i;
                   int b;
                   void funIn(){
                             funout();
                             System.out.println("funin of sinner");
                   }  } }
public class Main {
          public static void main(String[] args) {
                   sout.sinner soi= new sout.sinner();
                   soi.funIn(); }        }


Local innerclasses:-
An inner class which is defined inside a method is known as Local inner class. Local inner class act as a local variable. We should not use local variables of a function inside the local inner class, but we can use local constants.
Since local inner class is local to a function, we can create the object and call the function only inside that.
Program:-  

Output:-

finner of Linner
4
public class Louter {
          int i;
          void fouter()        {
                   int x=9;
                   final int y=4;
                   class Linner{
                             int d;                   
                             void finner(){
                                      System.out.println("finner of Linner");
                                      //System.out.println(x);
                                      //gives compilation error bcz x is a local variable
                                      System.out.println(y);
                             }        }
                   Linner in=new Linner();
                   in.finner();   }
          public static void main(String[] args) {
                   Louter out=new Louter();
                   out.fouter();         } }
Anonymous innerclass:-
An inner class without the class name is known as Anonymous inner class. To create an anonymous inner class we need either “interface or abstract class”.
2 types of anonymous innerclasses.
1.   Member anonymous innerclass
2.   Local anonymous innerclass
Member anonymous innerclass:-
interface Xyz    {
          void xone();
          void xtwo();    }
public class Anony   {
          static Xyz obj=new Xyz()   {
                   public void xone()
                   {
                             System.out.println("xone");

OutPut:

xone
xtwo

                   }
                   public void xtwo()
                   {
                             System.out.println("xtwo");
                   }    };
          public static void main(String[] args) {
                   Anony.obj.xone();
                   Anony.obj.xtwo();   }   }
Local anonymous innerclass:-
interface Ixy    {

OutPut:

xone of Ixy
xtwo of Ixy
          void xone();
          void xtwo();
}
 class Localanony    {
           static Ixy  getIxy()
           {
                    Ixy ixy=new Ixy()
                             {
                                      public void xone()
                                      {
                                                System.out.println("xone of Ixy");
                                      }
                                      public void xtwo()
                                      {
                                                System.out.println("xtwo of Ixy");
                                      }
                             };
                             return ixy;   }
          public static void main(String[] args)    {
                   Ixy ix=Localanony.getIxy();
                   ix.xone();
                   ix.xtwo();
          }   }
Factory method:-
A method which is used to create the object of a class from outside of the class even if the constructor is declared as private.
Factory methods always returns class object and it should be always public and static.
Program:-
class Factory  {
           int x;
           int y;
           private Factory()   {
                    System.out.println("private constructor");
           }
           public static Factory getFactory()   {
                    Factory fact =new Factory();
//we can call private constructor inside the class not outside the class.
                    return fact;

OutPut:

private constructor
mone
mtwo
           }
           public void mone()   {
                    System.out.println("mone");
           }
           public void mtwo()   {
                    System.out.println("mtwo");
           }
           public static void main(String[] args)    {
                    Factory factory=Factory.getFactory();
                    factory.mone();
                    factory.mtwo();   }   }


No comments:

Post a Comment