Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Polymorphism

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Polymorphism

    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 ..

    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
    Last edited by GregBrannon; March 5th, 2014 at 02:04 PM. Reason: Fixed code tags - no spaces and 'code' first


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Polymorphism

    Quote Originally Posted by gautammuktsar@gmail.com View Post
    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:

    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.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Not Understanding Polymorphism
    By superjacko in forum Java Theory & Questions
    Replies: 5
    Last Post: July 28th, 2013, 11:47 PM
  2. Inheritence and Polymorphism
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 1st, 2013, 07:59 AM
  3. Polymorphism Question
    By BuhRock in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 11:09 AM
  4. Polymorphism.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2012, 09:28 AM
  5. Polymorphism issue
    By LDM91 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 03:26 PM