Learn Anonymous Classes in JAVA
Java Anonymous Inner Class
- Anonymous inner classes of Java are called anonymous because they have no name.
- They are anonymous and inline.
- Anonymous classes are essentially inner classes and defined within some other classes.
- However, the way anonymous classes are written in Java code may look weird but anonymous inner classes facilitate programmers to declare and instantiate the class at the same time.
- Another important point to note about anonymous inner classes is that they can be used only once on the place they are coded.
- In other words, if you want to create only one sub-classed object of a class, then you need not to give the class a name and you can use anonymous inner class in such a case.
Anonymous Classes
- Anonymous inner classes can be defined not just within a method, but even within an argument to a method.
- Anonymous inner classes cannot have explicit constructors declared because they have no name to give the constructor.
How to Declare Java Anonymous Inner Classes
- Anonymous inner classes are defined at the same time they are instantiated with new.
- They are not declared as local classes are done rather anonymous inner classes are defined in the new expression itself, as part of a statement.
How to Declare Java Anonymous Inner Classes
- An anonymous inner class declaration expression looks like a constructor invocation, except that there is a class definition contained in a block of code.
- Before going into further details of anonymous inner classes we must understand that an anonymous inner class is not an independent inner class rather it is a sub-class of either a class type or an anonymous implementer of the specified interface type.
- So, when anonymous inner classes are in picture polymorphism must be there.
- And when polymorphism is there you can only call methods from parent class reference those are defined in the reference variable type.
- Java's anonymous inner classes being sub-classes or implementer do strictly adhere to the polymorphism rules.
Java Anonymous Inner Class of a Class Type
/* AnonymousClassDemo.java */public class AnonymousClassDemo {public static void main(String[] args) {Dog dog = new Dog() { public void someDog () { System.out.println("Anonymous Dog"); } }; // anonymous class body closes here //dog contains an object of anonymous subclass of Dog. dog.someDog(); } } class Dog { public void someDog() { System.out.println("Classic Dog"); } }
- Remember, anonymous inner classes are inherited ones, and we always use a superclass reference variable to refer to an anonymous subclass object.
- And, we can only call methods on an anonymous inner class object that are defined in the superclass.
- Though, we can introduce new methods in anonymous inner class, but we cannot access them through the reference variable of super-class because super-class does not know anything about new methods or data members introduced in subclass.
Java Anonymous Implementer of an Interface Type
- an anonymous inner class can implement an interface anonymously.
Thanks For Visiting Our Blog/* AnonymousInterfaceDemo.java */interface Manageable{ public void manage(); }public class AnonymousInterfaceDemo{ public static void main(String[] args) {Manageable m = new Manageable() {public void manage() {System.out.println("It is manageable"); } }; // anonymous interface implementer closes here //m contains an object of anonymous interface implementer of Manageable. m.manage(); } }
No comments:
Post a Comment