# Polymorphism

• March 5th, 2014, 12:36 PM
gautammuktsar@gmail.com
Polymorphism
Code java:

```class Bank{ int getRateOfInterest(){return 0;} }   class SBI extends Bank{ int getRateOfInterest(){return 8;} }   class ICICI extends Bank{ int getRateOfInterest(){return 7;} } class AXIS extends Bank{ int getRateOfInterest(){return 9;} }   class Test{ public static void main(String args[]){ Bank b1=new SBI(); Bank b2=new ICICI(); Bank b3=new AXIS(); System.out.println("SBI Rate of Interest: "+b1.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+b2.getRateOfInterest()); System.out.println("AXIS Rate of Interest: "+b3.getRateOfInterest()); } }```

We can also access getRateOfInterest() method by creating the reference of the subclass {here ICICI,SBI,AXIS} like this ..

Code java:

```class Test{ public static void main(String args[]){ SBI b1=new SBI(); ICICI b2=new ICICI(); AXIS b3=new AXIS(); System.out.println("SBI Rate of Interest: "+b1.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+b2.getRateOfInterest()); System.out.println("AXIS Rate of Interest: "+b3.getRateOfInterest()); } }```

By this version we get the same result but why we use 1st version means access the method by superclass reference ..Is there any rule under Polymorphism like this .. if it is then i don't know as i am new to java ..please explain me
• March 5th, 2014, 02:15 PM
andbin
Re: Polymorphism
Quote:

Originally Posted by gautammuktsar@gmail.com
By this version we get the same result but why we use 1st version means access the method by superclass reference ..Is there any rule under Polymorphism like this .. if it is then i don't know as i am new to java ..please explain me

For your simple Test class, there is no real difference between using the more generic Bank or the more specific sub types.
If you invoke getRateOfInterest on a Bank reference you do what is called a "polymorphic invocation": the version of the getRateOfInterest that is executed is choosen at runtime, basing on the real object that the reference points to.

So your doubt is: why use a Bank reference type? Simple: you can have a method that has a Bank parameter. Ideally it should work with any object that is a subtype of Bank. And you can have a method that has Bank as return type. It may return an object of one of the subtypes of Bank.

You can have, say, a method that selects the "best" between 2 Bank objects by its rate of interest:

Code :

```public static Bank selectBestBankByRate(Bank b1, Bank b2) { if (b1.getRateOfInterest() >= b2.getRateOfInterest()) { return b1; } else { return b2; } }```

This method doesn't know anything about any of the specific sub types of Bank. But it works with any of them. This is the real power of the polymorphism.

Final note: in theory, your Bank class can/should be "abstract", with an abstract getRateOfInterest (no body { ... }). Exactly like, other example, Animal class and Cat, Dog subclasses. It has no much sense to instantiate an Animal .... Animal what? Instead, Cat and Dog are more concrete types.