Tuesday, 30 May 2017

Tutorial 22: Anonymous Classes



We normally refer to something or someone as “anonymous” if their identity is unclear. For example, we can have people submitting their reviews anonymously, which means that they do something, but their identity is not clear.


In Java, we can create and use anonymous classes for one off activities. It is when we want something done, but we don’t want to define a fresh implementation for the class, because whatever we want this class to do is something we will need to do only once.

So let’s look at an example: Let’s say you create a class Person, and define a method myIdentity(), which outputs “I am a Person” on the console:


public class Person {



            public void myIdentity() {

                        System.out.println("I am a person");

            }

}


But what if, when we are creating a Person object, we want that particular object to output the following text on the console: “Hello”? You see, we want this just to be output for the current object we are creating, not for every object of type Person that is created after that. This is where an anonymous class comes in:


public class Person {



            public void myIdentity() {

                        System.out.println("I am a person");

            }



            public static void main(String[] args) {

                        Person person1 = new Person() {

                                    @Override

                                    public void myIdentity() {

                                                System.out.println("Hello");

                                    };

                        };



                        person1.myIdentity();

            }

}

If you run this program, Java will output the text “Hello” on the console.

 Figure 1: An anonymous class.


As you can see, the class that we have defined in the main function is of type Person. It does something for us. However, we cannot really say what its name is. And this is why it is known as an anonymous class.


Let us look at another scenario. Let’s say that you have a function in the class Person that outputs the text: “I am a Person”. However, when we are creating a particular Person object, we would like it to output the text “I am an object of type Person.” So the class would look like this: 


public class Person {

            public void whomAmI() {

                        System.out.println("I am a Person");

            }



            public static void main(String[] args) {

                        Person person1 = new Person();

                        person1.whomAmI();

                        Person person2 = new Person() {

                                    @Override

                                    public void whomAmI() {

                                                System.out.println("I am an object of type Person");

                                    };



                        };

                        person2.whomAmI();

            }
} 

Figure 2: Another example of using anonymous classes. 


As you can see, we have created an anonymous class that overrides the method whomAmI(); and outputs something different from what the original function outputs.

Try practicing creating your own anonymous classes, and see what exciting things you can do with them.


This is all about anonymous classes for now. In the next tutorial, we look at interfaces in Java.


If you have any question pertaining to this tutorial or other tutorials, please drop them in the comment section and I will make sure to address it.

Until next time, stay safe. 

No comments:

Post a Comment