Anonymous Inner Class in Java


A class is a non-primitive or user-defined data type in Java, while an object is an instance of a class. To define a class within another class. Such a class is called a Nested Class. An anonymous it is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.

Source Code

//Anonymous Inner Class
 
abstract class testDemo {
    abstract void display();
}
 
class outerDemo {
    public void outerDisplay() {
        testDemo o =new testDemo() {
            @Override
            public void display() {
                System.out.println("Test Display");
            }
        };
        o.display();
    }
}
 
 
public class anonymousInner {
    public static void main(String[] args) {
        outerDemo o =new outerDemo();
        o.outerDisplay();
    }
}
 

Output

Test Display
To download raw file Click Here
`

Basic Programs