Java – Inner classes

Java supports nesting classes; a class can be a member of another class. Creating an inner class is quite simple. Just write a class within a class. Unlike a class, an inner class can be private. Once you declare an inner class private, it cannot be accessed from an :

Nested Classes

In java have methods variables of a class have another class as its member. class can be called the nested class, and when class support with inner class is defined the outer class. Syntax the class Outer_test is the outer class and the class Inner_test is the nested class.

  class Outer_somthingcode {
   class Nested_somethingcode {
   }
}

Nested classes are can be two cataloger:

  • Non-static nested classes non-static members of a class.
  • Static nested classes static members of a class.

Inner Classes (Non-static Nested Classes)

The class as a member of other class, then the inner class can be made private. Inner class are three type depending on where you want use:

  • Inner Class
  • Method-local Inner Class
  • Anonymous Inner Class

Inner Class An inner class can be private and but once you declare an inner class private, it cannot be accessed from an object outside the class.

Example:

class aditya {
  int id;
  aditya(int i) {
    id = i;
    aditya b = new aditya();
    b.think();
  }

  private class aditya {
    public void think() {
      System.out.println(id + " is thinking");
    }
  }
}

an inner class can be private. Once you declare an inner class private, it cannot be accessed from an object outside the class.

Output

This is an inner class.

Accessing the Private Members

getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class. To instantiate the inner class, initially you have to instantiate the outer class. Thereafter, using the object of the outer class, following is the way in which you can instantiate the inner class.

Outer_Somethingcode outer = new Outer_Somethingprint();
Outer_Somethingcode.Inner_Somethingcode inner = outer.new Inner_Somethingprint();

Example:

class Outer_Somthingcode {
   // private variable of the outer class
   private int num = 200;  
   
   // inner class
   public class Inner_Somethingcode {
      public int getNum() {
         System.out.println("this is inner class");
         return num;
      }
   }
}

Output

This is the getnum method of the inner class 200

Method-local Inner Class

local variables are very helpful the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined.

public class Outerclass {
   // instance method of the outer class 
   void my_Method() {
      int num = 28;

      // method-local inner class
      class MethodInner_Somethingprint {
         public void print() {
            System.out.println("This is method inner class "+num);    
         }   
      } // end of inner class
    
      // Accessing the inner class
      MethodInner_Somethingprint inner = new MethodInner_Somethingcode();
      inner.print();
   }
   
   public static void main(String args[]) {
      Outerclass outer = new Outerclass();
      outer.my_Method();        
   }
}

Output

This is method inner class 23

Anonymous Inner Class

An inner class declared without a class name is known as an anonymous inner classthey are mainly used whenever you need to override the method of a class or an interface.

AnonymousInner an_inner = new AnonymousInner() {
   public void my_method() {
      ........
      ........
   }   
};

Example:

abstract class AnonymousInner {
   public abstract void mymethod();
}

public class Outer_class {

   public static void main(String args[]) {
      AnonymousInner inner = new AnonymousInner() {
         public void mymethod() {
            System.out.println("This is an example of anonymous inner class");
         }
      };
      inner.mymethod(); 
   }
}

Anonymous Inner Class as Argument

a method accepts an object of an interface, an abstract class, or a concrete class, then we can implement the interface, extend the abstract class, and pass the object to the method. If it is a class, then we can directly pass it to the method.

Example:

// interface
interface Message {
   String greet();
}

public class My_class {
   // method which accepts the object of interface Message
   public void displayMessage(Message m) {
      System.out.println(m.greet() +
         ", This is an example of anonymous inner class as an argument");  
   }

   public static void main(String args[]) {
      // Instantiating the class
      My_class obj = new My_class();

      // Passing an anonymous inner class as an argument
      obj.displayMessage(new Message() {
         public String greet() {
            return "Hello Java";
         }
      });
   }
}
Related Post