Runtime Polymorphism in java
Runtime Polymorphism:
--------------------------------------------------
---------------------------------------------------
Polymorphism is the foundation of Object Oriented Programming. It means that one object can be have as another project. So how does on object can become other, its possible through following
Inheritance
Overriding/Implementing parent Class behavior
Runtime Object binding
One of the main advantage of it is switch implementations. Lets say you are coding an application which needs to talk to a database. And you happen to define a class which does this database operation for you and its expected to do certain operations such as Add, Delete, Modify. You know that database can be implemented in many ways, it could be talking to file system or a RDBM server such as MySQL etc. So you as programmer, would define an interface that you could use, such as...public interface DBOperation { public void addEmployee(Employee newEmployee); public void modifyEmployee(int id, Employee newInfo); public void deleteEmployee(int id); }
Now you may have multiple implementations, lets say we have one for RDBMS and other for direct file-systempublic class DBOperation_RDBMS implements DBOperation // implements DBOperation above stating that you intend to implement all // methods in DBOperation public void addEmployee(Employee newEmployee) { // here I would get JDBC (Java's Interface to RDBMS) handle // add an entry into database table. } public void modifyEmployee(int id, Employee newInfo) { // here I use JDBC handle to modify employee, and id to index to employee } public void deleteEmployee(int id) { // here I would use JDBC handle to delete an entry } }
Lets have File System database implementationpublic class DBOperation_FileSystem implements DBOperation public void addEmployee(Employee newEmployee) { // here I would Create a file and add a Employee record in to it } public void modifyEmployee(int id, Employee newInfo) { // here I would open file, search for record and change values } public void deleteEmployee(int id) { // here I search entry by id, and delete the record } }
Lets see how main can switch between the twopublic class Main { public static void main(String[] args) throws Exception { Employee emp = new Employee(); ... set employee information DBOperation dboper = null; // declare your db operation object, not there is no instance // associated with it if(args[0].equals("use_rdbms")) { dboper = new DBOperation_RDBMS(); // here conditionally, i.e when first argument to program is // use_rdbms, we instantiate RDBM implementation and associate // with variable dboper, which delcared as DBOperation. // this is where runtime binding of polymorphism kicks in // JVM is allowing this assignment because DBOperation_RDBMS // has a "is a" relationship with DBOperation. } else if(args[0].equals("use_fs")) { dboper = new DBOperation_FileSystem(); // similarly here conditionally we assign a different instance. } else { throw new RuntimeException("Dont know which implemnation to use"); } dboper.addEmployee(emp); // now dboper is refering to one of the implementation // based on the if conditions above // by this point JVM knows dboper variable is associated with // 'a' implemenation, and it will call appropriate method } }
Comments
Post a Comment