What is Anonymous inner class?
When any inner class is defined without any name it is called as Anonymous inner class. Anonymous inner class also refer as name-less inner class.
From 4 categories of inner classes, Anonymous inner classes is most frequently used inner class in java.
Based on declaration and behavior there are 3 types of inner classes.
- Anonymous inner class that extends a class
- Anonymous inner class that implements interface
- Anonymous inner class that defined inside arguments
So lets see above three types in details with examples :
1. Anonymous inner class that extends a class
We can extends another class using anonymous inner class. like if we don't want to create class that extends another class all the time, then we can use anonymous inner class that extends a class.
Lets see simple example of both extending thread using another class and creating anonymous inner class that extends a class.
Example 1 : Extending Thread class using normal class
class MyThread extends Thread {
public void run() {
System.out.println("Child Thread");
}
}
class Solution {
public static void main(String[] args) {
MyThread obj = new MyThread();
obj.start();
}
}
Example 2 : Extending Thread class using anonymous inner class
class AnonymousInnerClass {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
System.out.println("Child Thread");
}
};
t.start();
}
}
In above example, if we do not create anonymous inner class then we have to create another class that extends Thread class.
Lets see another example how we can override method from anonymous inner class.
Example 3 : Override method from anonymous inner class.
class AnonymousInnerClass {
public void methodOne() {
System.out.println("methodOne");
}
public void methodTwo() {
System.out.println("methodTwo");
}
}
class Solution {
public static void main(String[] args) {
// Here we are using Anonymous Inner class, that extends a class (AnonymousInnerClass)
// Creating Anonymous Inner Class and Overriding methodOne()
AnonymousInnerClass obj = new AnonymousInnerClass() {
public void methodOne() {
System.out.println("Anonymous Inner Class : methodOne");
}
};
obj.methodOne();
obj.methodTwo();
// For calling AnonymousInnerClass methodOne()
AnonymousInnerClass obj1 = new AnonymousInnerClass();
obj1.methodOne();
}
}
Output :
Anonymous Inner Class : methodOne
methodTwo
methodOne
When we compile above code compiler generates 3 .class files :
- AnonymousInnerClass.class
- Solution.class
- Solution$1.class
2. Anonymous inner class that implements an interface
We can also implements interface using anonymous inner class. Lets see example of implements Runnable interface using inner class to create a Thread.
We will seen both normal implementation and inner class approach to implements Runnable interface.
Example 4 : Implements Runnable interface using normal class
class MyRunnable implements Runnable {
public void run() {
System.out.println("Inside run method");
}
}
class Solution {
public static void main(String[] args) {
MyRunnable obj = new MyRunnable();
Thread t = new Thread(obj);
t.start();
}
}
Example 5 : Implements Runnable interface using anonymous inner class
class MyRunnable {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
System.out.println("Inside run method");
}
};
Thread t = new Thread(r);
t.start();
}
}
Now lets see 3rd way to define anonymous inner class.
3. Anonymous inner class that defined inside arguments
We can also defined inner class inside method or constructor arguments. lets see example of that so you can understand easily.
Example 6 : Anonymous inner class that defined inside arguments
class ThreadDemo {
public static void main(String[] args) {
new Thread( new Runnable() {
public void run() {
System.out.println("Child thread");
}
}).start();
System.out.println("Main thread");
}
}
Points to remember for Anonymous inner class
- In normal class, we can implements any number of interface but in Anonymous inner class can implements only one interface at a time.
- In normal class, we can extends one class and implements many interface at same time but in Anonymous inner class can extends a class or can implements an interface but not simultaneously.
- In anonymous inner class we can not write any constructor explicitly. because name of class and name of constructor must be same but anonymous inner class does not have any name.
Happy Learning... Happy Coding..
Other articles :
- How to call inner class method from static, non static method and outside of outer class
- Java Lambda Expressions HackerRank solution with Explanation
Comments
Post a Comment