Inner Class Concept in Java

0

Using this concept we will create class inside class ,It is also called Nested Class:-
1 Dynamic Inner class:-
   It Contain Dynamic data member and member function.
  class A
 {
       class B
       {

      }
}
Example of Inner Class:-
class A
{
    class B
    {
         public void fun1()
         {
             System.out.println("B");
         }
    }
  public void fun()
         {
             System.out.println("A");
         }

public static void main(String args[])
{
     A obj = new A();
     obj.fun();
     obj.new B().fun1();
}
}
2 Static Inner Class:-
     It Contain Static Data member and member function.
   class A
 {
       static class B
       {

      }
}

Example of Static Inner Class in Java:-
class A
{
   static class B
    {
         public static void fun1()
         {
             System.out.println("B");
         }
    }
  public void fun()
         {
             System.out.println("A");
         }

public static void main(String args[])
{
     A obj = new A();
     obj.fun();
     A.B.fun1();
}
}Anonymous class in Java:-
If we want to define the functionality of Interface and abstract class under any method block instantly then we can create an anonymous class.
Example of it's:
abstract class An
{
   public abstract void fun();
}
interface In
{
    void fun1();
}
class Bn 
{
   public static void main(String args[])
   {
      An obj=  new An(){

             public void fun()
                 {
                         System.out.println("Bn");
                }
            };
      In o = new In()
      {
                public void fun1()
                 { 
                         System.out.println("In");
                }
      };
      obj.fun();
      o.fun1();
   }
}
Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)