A nested class that doesn't have any name is known as an anonymous class.
xxxxxxxxxx
class Polygon {
public void display() {
System.out.println("Inside the Polgon class");
}
}
class AnonymousDemo {
public void createClass() {
Polygon p1 = new Polygon() {
public void display() {
System.out.println("Inside an anonymous class.")
}
};
p1.display();
}
}
class Main {
public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
}
}
// output:
// Inside an anonymous class.
x
interface Polygon {
public void display();
}
class AnonymousDemo {
public void createClass() {
Polygon p1 = new Polygon() {
public void display() {
System.out.println("Inside an anonymous class.");
}
};
p1.display();
}
}
class Main {
public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
}
}
// output:
// Inside an anonymous class.
In this example, we created an anonymous class that implements thePolygon
interface.