Tuesday 14 May 2013

what are the different ways to implement Anonymous inner class?

 Different approaches to implement Anonymous inner class

In the earlier post, I mentioned about what is inner or nested classes and how to use those classes. Please have a look for the reference in the below link.


Now I will explain what are the different ways through which we can implement anonymous classes?

Anonymous inner classes are very useful in some places :- think about a situation where we need to create an instance of an object without creating subclass of a class and also performing some related additional tasks like method overloading.


In JDBC, generally we are doing as below:-

Connection con = DriverManager.getConnection(“”);
Connection is an interface which can’t be instantiated but can be referenced. Connection is an interface from java.sql package, for which getConnection() returns an anonymous inner class object for the Connection interface.
 

We have few approaches through which we can create anonymous classes.
Below are the approaches to implement Anonymous inner class:-
  •  Anonymous inner class by an abstract class
  •  Anonymous inner class by interface
  •  Implementing anonymous inner class as an argument


FIRST APPROACH
  • Anonymous inner class by an abstract class
Example for the first approach implementation


package com.gaurav.corejava.innerclass;

/**This is an abstract class where getValue() method is abstract;*/
abstract class ParentClass {
            abstract void getValue();

            public String getFunction() {
                        System.out.println("INSIDE GETFUNCTION() METHOD");
                        return "HELLO INDIA";
            }

}

/**
 * As we can't directly create an instance of abstract class, here i am
 * providing the implementation of the abstract class by using Anonymous inner
 * class
 */
public class DefAnonyClassFirstApproach {
            /** IMPLEMENTATION OF THE ABSTRACT METHOD USING ANONYMOUS
      * CLASS */
            ParentClass pc = new ParentClass() {
                        void getValue() {
                                    System.out.println("THIS IS THE IMPLEMENTATION OF THE ABSTRACT GETVALUE() METHOD");
                                    System.out.println("INSIDE GETVALUE() METHOD");
                                    System.out.println("GETFUNCTION METHOD CALL : - " + pc.getFunction());
                        }
            };

            public static void main(String args[]) {
                        DefAnonyClassFirstApproach dcfa = new DefAnonyClassFirstApproach();
                        dcfa.pc.getValue();
            }
}

Result:-

THIS IS THE IMPLEMENTATION OF THE ABSTRACT GETVALUE() METHOD
INSIDE GETVALUE() METHOD
INSIDE GETFUNCTION() METHOD
GETFUNCTION METHOD CALL : - HELLO INDIA



SECOND APPROACH
  •  Anonymous inner class by interface
Example for the second approach implementation

package com.gaurav.corejava.innerclass;

/** This is an interface where getSoftwareCompanies() method is abstract; */
interface Jobs {
            String getSoftwareCompanies();
}

public class DefAnonyClassSecondApproach {
            /**
             * Here, I am creating an instance of interface JOBS by providing the
             * implementation of the abstract method using Anonymous inner class
             */
            Jobs jobs = new Jobs() {
                        public String getSoftwareCompanies() {
                                    System.out.println("INSIDE GETSOFTWARECOMPANIES() METHOD");
                                    return "MICROSOFT";
                        }
            };

            public static void main(String args[]) {
                        DefAnonyClassSecondApproach dcsa = new DefAnonyClassSecondApproach();
                        System.out.println("JOB COMPANY NAME IS : - "
                                                + dcsa.jobs.getSoftwareCompanies());
            }
}

Result:-

INSIDE GETSOFTWARECOMPANIES() METHOD
JOB COMPANY NAME IS : - MICROSOFT



THIRD APPROACH
  •  Implementing anonymous inner class as an argument
Example for the third approach implementation

package com.gaurav.corejava.innerclass;

interface CommunicationMedium {
            public String getJourneyMedium();
}

class JourneyDestination {
            String strJourneyWay = "NO MEDIUM";

            public String getJourneyWay(CommunicationMedium c) {

                        if (c.getJourneyMedium().equals("SHIP"))
                                    strJourneyWay = "WATER";
                        if (c.getJourneyMedium().equals("AEROPLANE"))
                                    strJourneyWay = "AIR";
                        if (c.getJourneyMedium().equals("ROAD"))
                                    strJourneyWay = "CAR";
                        return strJourneyWay;

            }
}

public class DefAnonyClassThirdApproach {
            String destinationPlace = "NO DESTINATION";

            String getTheJourneyDestination() {
                        JourneyDestination jd = new JourneyDestination();
                        jd.getJourneyWay(new CommunicationMedium() {
                                    public String getJourneyMedium() {
                                                return "SHIP";
                                    }
                        });
                        System.out.println("THE COMMUNICATION MEDIUM IS : - " + jd.strJourneyWay);
                        if (jd.strJourneyWay.equalsIgnoreCase("AIR"))
                                    destinationPlace = "HEDERABAD";
                        else if (jd.strJourneyWay.equalsIgnoreCase("WATER"))
                                    destinationPlace = "KOLKATA";
                        else if (jd.strJourneyWay.equalsIgnoreCase("CAR"))
                                    destinationPlace = "DELHI";
                        return destinationPlace;
            }

            public static void main(String args[]) {
                        DefAnonyClassThirdApproach dacta = new DefAnonyClassThirdApproach();
                        System.out.println("THE DESTINATION PLACE IS : - "
                                                + dacta.getTheJourneyDestination());
            }
}

Result:-

THE COMMUNICATION MEDIUM IS : - WATER
THE DESTINATION PLACE IS : - KOLKATA


No comments:

Post a Comment