Online Tutorials & Training Materials | STechies.com
Register Login

Polymorphism in Java

|| || 0

Polymorphism in Java
Stechies

What is Polymorphism?

Polymorphism is the ability of an object to have a different form. So polymorphism literally means the word polymorphism means having different forms and whenever you define an object, class object I should say and its property and quality having many forms is called polymorphism.

How we can make our classes polymorphism or how you can define an object which follows polymorphism.

Here I am going to give an example.

So I have four classes defined. One is called the bank class, which will be my base class and this bank class contain three or one methods.

package lesson1;

public class Bank {
   int getInterestRate() {
         return 0;
   }
 }

This base class method just gives the rate of interest so we will define this method as get interest rate. And because it’s a base class It will be returning 0 as an interest.

We also have defined Bank_ABC class, Bank_DEF class and Bank_XYZ class.

So if you don’t know how to make a class, just right click new, go to class, and in here name the class. So this is how I have created these classes.

So one is called the bank class which will be my main class.

Second, class is called Bank_ABC, which extends from bank class because bank class is our base class and Bank_ABC is our derived class. And this also contains the same method but this returns the rate of interest or this bank has a rate of interest = 5%.

package lesson1;

 public class_BankABC extends Bank {
         int getInterestRate() {
             return 5;
         }
 }

Bank_DEF, which also extends from Bank class have the rate of interest 6%. So I have defined a method here which returns 6%. Simple!

package lesson1;

 public class_BankDEF extends Bank {
         int getInterestRate() {
             return 6;
         }
  }

And Bank class XYZ which is also extending from Bank. So this is the derived class and this is the base class also have same method get interest rate and the interest rate in this bank is 10%.

package lesson1;

 public class_BankXYZ extends Bank {
        int getInterestRate() {
             return 10;
         }
 }

So I have the same method in all the four classes get interest rate and the only difference in this function is the rate of interest. The main bank itself has 0, Bank ABC has 5% interest rate, Bank DEF has 6% interest rate and Bank XYZ has 10 % interest rate.

Now there is a property in Java called polymorphism by which you can define the reference or you can point the reference to a base class to any object of the derived class. So what I mean by that is so when reference object or reference of a parent class points to the object of the sub-class it’s called upcasting. And this is the basic you know extract of polymorphism. So our, we will define the reference of our Bank class where we will just define Bank b1 = or Bank abc=. So this is the reference to the Bank class which will point to object of Bank ABC, which is a child class.

package lesson1;

 public class MyClass {
    public static void main(string[] args) {
      Bank abc = new Bank_ABC();
     }

 }

and this is what we call polymorphism. When an object can have different form So the object of Bank class is having the form of Bank ABC.

So the reference of the Bank class pointing to the object of Bank ABC class. In the same way, we can define different objects from here. You can define Bank def and here also Bank DEF and Bank xyz and Bank XYZ..

package lesson1;

 public class MyClass {
    public static void main(string[] args) {
        Bank abc = new Bank_ABC();
        Bank def = new Bank_DEF();
        Bank xyz = new Bank_XYZ();
       }

}

So and let’s see it had the same name or not. This should be underscored. So we have defined three references of bank class itself which points to the subclasses or object of subclasses. Reference abc points to the object of the Bank_ABC class, reference def points to the reference object of DEF class and reference xyz points to object XYZ class. And I can call all these methods get interest rate from all these references. So I cn just write syso ctlr shift and in the same way I will do three times. and first time I will take abc as my object and I will just call get interest rate and in the second statement I will paste def.getinterestrate and third time I will paste xyz as my instance and I will call getinterestrate.

package lesson1;

 public class MyClass {
    public static void main(string[] args) {
        Bank abc = new Bank_ABC();
        Bank def = new Bank_DEF();
        Bank xyz = new Bank_XYZ();

        System.out.print1n(abc.getInterestRate());
        System.out.print1n(def.getInterestRate());
        System.out.print1n(xyz.getInterestRate());

      }
 }

And when I run the program I get 5,6 and 10.

package lesson1;

 public class MyClass {
    public static void main(string[] args) {
        Bank abc = new Bank_ABC();
        Bank def = new Bank_DEF();
        Bank xyz = new Bank_XYZ();

        System.out.print1n(abc.getInterestRate());
        System.out.print1n(def.getInterestRate());
        System.out.print1n(xyz.getInterestRate());

      }

So this type of reference defining reference from the object of subclasses is called polymorphism and all you know member, functions are available through this reference. So this get interest rate are available in here in all the classes. And this Bank ABC gives 5% interest rate so it is return 5. Bank DEF gives 6% interest rate so it returns and Bank XYZ gives 10% interest rate so it returns 10. But the interesting thing here is all are the object of bank or all reference are pointing to different objects but all objects are from bank class.

So this is how polymorphism work in java.


Related Articles