Anonymous inner classes
In this tutorial, we are going to discuss about Anonymous inner classes in java. Sometimes we can declare inner class without name such type of inner classes are called anonymous inner classes.
Anonymous inner classes in Java are a type of inner class that do not have a name and are declared and instantiated at the same time.
The main objective of anonymous inner classes is “just for instant use”. There are 3 types of anonymous inner classes
- Anonymous inner class that extends a class.
- Anonymous inner class that implements an interface.
- Anonymous inner class that defined inside method arguments.
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
class PopCorn {
public void taste() {
System.out.println("Pop Corn is Spicy");
}
}
public class Test {
public static void main(String[] args) {
PopCorn p = new PopCorn() {
public void taste() {
System.out.println("Pop Corn is Salty");
}
};
p.taste();
PopCorn p1 = new PopCorn();
p1.taste();
}
}
Output
Pop Corn is Salty
Pop Corn is Spicy
PopCorn p = new PopCorn();
We are just creating a PopCorn object.
PopCorn p=new PopCorn() {
};
We are creating child class without name for the PopCorn class and for that child class we are creating an object with Parent PopCorn reference.
PopCorn p=new PopCorn() {
public void taste() {
System.out.println("salty");
}
};
- We are creating child class for PopCorn without name.
- We are overriding taste() method.
- We are creating object for that child class with parent reference.
Note
Inside Anonymous inner classes we can take or declare new methods but outside of anonymous inner classes we can’t call these methods directly because we are depending on parent reference.[parent reference can be used to hold child class object but by using that reference we can’t call child specific methods]. These methods just for internal purpose only.
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
class PopCorn {
public void taste() {
System.out.println("Pop Corn is Spicy");
}
}
public class Test {
public static void main(String[] args) {
PopCorn p = new PopCorn() {
public void taste() {
methodOne();
System.out.println("Pop Corn is Salty");
}
public void methodOne() {
System.out.println("Child specific method");
}
};
// p.methodOne(); // C.E
p.taste(); // Pop Corn is Spicy
PopCorn p1 = new PopCorn();
p1.taste(); // Pop Corn is Salty
}
}
Anonymous Inner Class that implements an interface
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
public class Test {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Child thread");
}
}
};
Thread t = new Thread(r);
t.start();
for (int i = 0; i < 5; i++) {
System.out.println("Main thread");
}
}
}
Output
Main thread
Main thread
Main thread
Main thread
Main thread
Child thread
Child thread
Child thread
Child thread
Child thread
Anonymous Inner Class that define inside method arguments
package com.ashok.innerclasses;
/**
*
* @author ashok.mariyala
*
*/
class Test {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Child thread");
}
}
}).start();
for (int i = 0; i < 5; i++) {
System.out.println("Main thread");
}
}
}
Output
main thread
main thread
main thread
main thread
main thread
child thread
child thread
child thread
child thread
child thread
Key Points:
- Anonymous inner classes are useful for creating short-lived objects with specific behaviors.
- They are defined and instantiated at the same time, without needing to create a separate class declaration.
- Anonymous inner classes can access outer class variables and methods just like regular inner classes.
- They are commonly used in event handling, thread creation, and other scenarios where a temporary subclass or implementation is needed.
That’s all about the Anonymous inner classes in java. If you have any queries or feedback, please write email us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!