TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Friday, 27 December 2019

inner class in java in java javatpoint | anonymous inner class in java in hindi

Inner class in java

Inner class means one class which is a member of another class. There are basically four types of inner classes in java.

1)Nested inner class
2)Method Local inner class
3)Static nested class
4) Anonymous inner classes

1.  Nested Inner class-Nested inner class can access any private instance variable of outer class. Like any other instance variable, we can have access modifier private, protected, public and default modifier.

 
anonymous inner class in java
inner class in java

Example demonstrates a nested class.

class Outer {
   // Simple nested inner class
   class Inner {
      public void show() {
           System.out.println("In a nested class method");
      }
   }
}
class Main {
   public static void main(String[] args) {
       Outer.Inner in = new Outer().new Inner();
       in.show();
   }
}

Output:

In a nested class method
Example:
class Outer {
void outerMethod() {
          System.out.println("inside outerMethod");
}
class Inner {
          public static void main(String[] args){
                    System.out.println("inside inner class Method");
          }
}
}
Output:
Error illegal static declaration in inner class
Output.Inner public static void main(String[] agrs)
Modifier  ‘static’ is only allowed in constant
Variable declaration
2. Method Local inner classes
Inner class can be declared within a method of an outer class. In the following example, Inner is an inner class in outerMethod().
class Outer {
    void outerMethod() {
        System.out.println("inside outerMethod");
        // Inner class is local to outerMethod()
        class Inner {
            void innerMethod() {
                System.out.println("inside innerMethod");
            }
        }
        Inner y = new Inner();
        y.innerMethod();
    }
}
class MethodDemo {
    public static void main(String[] args) {
        Outer x = new Outer();
        x.outerMethod();
    }
}

Output:

Inside outerMethod
Inside innerMethod

Method Local inner classes can’t use local variable of outer method until that local variable is not declared as final. For example, the following code generates compiler error (Note that x is not final in outerMethod() and innerMethod() tries to access it)

class Outer {
   void outerMethod() {
      int x = 98;
      System.out.println("inside outerMethod");
      class Inner {
         void innerMethod() {
            System.out.println("x= "+x);
         }
      }
      Inner y = new Inner();
      y.innerMethod();
   }
}
class MethodLocalVariableDemo {
   public static void main(String[] args) {
      Outer x=new Outer();
      x.outerMethod();
   }
}

Output:

Local variable x is accessed from within inner class;
needs to be declared final.

3.Static Nested class

Static nested classes are not technically an inner class. They are like a static member of outer class.

class Outer {
   private static void outerMethod() {
     System.out.println("inside outerMethod");
   }
     
   // A static inner class
   static class Inner {
     public static void main(String[] args) {
        System.out.println("inside inner class Method");
        outerMethod();
     }
   }
  
}

Output:

inside inner class Method
inside outerMethod

4. Anonymous inner classes

Anonymous inner classes are declared without any name at all. They are created in two ways.

a) As subclass of specified type


class Demo {
   void show() {
      System.out.println("i am in show method of super class");
   }
}
class Flavor1Demo {
  
   //  An anonymous class with Demo as base class
   static Demo d = new Demo() {
       void show() {
           super.show();
           System.out.println("i am in Flavor1Demo class");
       }
   };
   public static void main(String[] args){
       d.show();
   }
}

Output:

i am in show method of super class
i am in Flavor1Demo class
we have two class Demo and Flavor1Demo. Here demo act as super class and anonymous class acts as a subclass, both classes have a method show(). In anonymous class show() method is overridden.

a) As implementer of the specified interface

class Flavor2Demo {
  
    // An anonymous class that implements Hello interface
    static Hello h = new Hello() {
        public void show() {
            System.out.println("i am in anonymous class");
        }
    };
  
    public static void main(String[] args) {
        h.show();
    }
}
  
interface Hello {
    void show();
}

Output:

i am in anonymous class
 

No comments:

Post a Comment