Sunday, September 15, 2013

The Best Example ever on Runtime Polimorphism ...with JDBC code...

Hi,

I would like to explain you about runtime polymorphism with better example.

Here I am taking JDBC code to explain.


public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException, SQLException {
      
        //
preparing parameters
        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost/nag";// here in the url nag
                                                    // indicates database name
 

        // writing jdbc
        //DriverManager.registerDriver( new com.mysql.jdbc.Driver());


        Class.forName("com.mysql.jdbc.Driver");//
Loading of class at runtime
        Connection conn = DriverManager.getConnection(url, username, password);
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from emptest");
        while (resultSet.next()) {
            //
            System.out.println("employee number is :" + resultSet.getInt("eno"));
            System.out.println("employee name is:" + resultSet.getString("ename"));
            System.out.println("employee salary is :"+ resultSet.getInt("esal"));
            System.out.println();
            //
        }
    }
//end method



The above concept is known as Dynamic method Dispatching ( calling implemented class / child class objects method using it's super / interface reference ).

In the above code Driver is loaded using Class.forName() method, this method loads the Driver class dynamically. Till now it is ok.

What about next line, getting Connection object using DriverManger.getConnection() method, the method getConnection() is available in DriverManager class and  the method createStatement() is available in Connection interface.

It means we are calling a method using interface instance,

       Statement statement = conn.createStatement();

Then how this method is calling even though it is of Connection interface (abstract method), It is able to call that method.

This technique is called as Dynamic method Dispatching.

Writing the above code using Class.forName() is the correct example for Dynamic method Dispatching, Because the compiler doesn't know what Driver class will be loaded.

The decision of calling a method will be taken at runtime by JVM, not compiler.

But  writing the above code using DriverManager.registerDriver() is not the exact example for Dynamic method Dispatching, Because the compiler knows what Driver class will be loaded and checks for compilation.

The decision of calling a method will be taken at compile-time,,, :P

Please let me know any mistakes are in this post, Send your feedback at javaojavablog@googlegroups.com  or  nagarjuna.lingala@gmail.com


Note:
- If you use DriverManager.registerDriver(), compiler checks for the Driver class in the classpath, if that class Does not exist in the classpath then compiler will throw an exception.

- If you use Class.forName(), compiler won't check for the specified Driver class in the classpath, and it throws ClassNotFoundException, this exception is checked exception, If the Driver class does not exist in the classpath at application running time then jvm will throw runtime exception only.

No comments:

Post a Comment