layout | title | parent | nav_order |
---|---|---|---|
default |
Anonymous Inner Class |
Inner Class |
2 |
A class that have no name is known as anonymous inner class in java. It should be used if you have to override method of class or interface. Java Anonymous inner class can be created by two ways:
1- Class (may be abstract or concrete).
2- Interface
abstract class Person{
abstract void eat();
}
class TestAnonymousInner{
public static void main(String args[]){
Person p=new Person(){
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
Output
nice fruits