Java - OOP - Inner Class

In Java, an inner class is a class that is defined within another class. Inner classes can access the members of the outer class, including private members, and can also define its own members. Inner classes are used to logically group classes and interfaces in one place, making the code more readable and maintainable.

Types of Inner Classes:

  • Non-Static Inner Class (Inner Class)
  • Static Nested Class
  • Local Inner Class
  • Anonymous Inner Class
  • Non-Static Inner Class (Inner Class):

Non-Static Inner Class (Inner Class)

A non-static inner class is also called an inner class. It is defined within another class and is associated with an instance of the outer class. Inner classes have access to all members of the outer class, including private members.

class OuterClass {
    int num;
    // Inner class
    private class InnerClass {
        public void print() {
            System.out.println("This is an inner class");
        }
    }
    // Accessing the inner class
    public void accessInnerClass() {
        InnerClass inner = new InnerClass();
        inner.print();
    }
}
public class Main {
    public static void main(String[] args) {
        // Creating an object of the outer class
        OuterClass outer = new OuterClass();
        // Accessing the inner class
        outer.accessInnerClass();
    }
}

Static Nested Class:

A static nested class is also defined within another class, but unlike non-static inner classes, it is a static member of the outer class. Static nested classes are used to create a namespace for the static methods and fields of the outer class.

class OuterClass {
    static int num = 5;
    // Static nested class
    static class StaticNestedClass {
        public void print() {
            System.out.println("This is a static nested class");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        // Accessing the static nested class
        OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass();
        nested.print();
    }
}

Local Inner Class:

A local inner class is defined within a method or a block of code. Local inner classes are not accessible outside of the method or block of code in which they are defined.

public class Main {
    public void print() {
        // Local inner class
        class LocalInnerClass {
            public void display() {
                System.out.println("This is a local inner class");
            }
        }
        // Accessing the local inner class
        LocalInnerClass inner = new LocalInnerClass();
        inner.display();
    }
    public static void main(String[] args) {
        Main outer = new Main();
        outer.print();
    }
}

In Java, an inner class is a class that is defined inside another class. Inner classes can be classified into two types: anonymous inner classes and non-static inner classes (also known as simply inner classes).

Anonymous Inner Class:

An anonymous inner class is a local class without a name that is used to create an instance of a class or an interface. It is usually used when we need to override the methods of a class or interface without creating a separate class for it. Anonymous inner classes are created and used only once and cannot be reused. They are defined using the new keyword along with a class or an interface.

interface Animal {
    void makeSound();
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal() {
            @Override
            public void makeSound() {
                System.out.println("Meow");
            }
        };
        animal.makeSound();
    }
}

In the above example, we define an anonymous inner class that implements the Animal interface and overrides its makeSound() method. We then create an instance of this anonymous class and call its makeSound() method.