Polymorphism 

Polymorphism
  • Okay, there is one final feature of objects that you need to understand. 
  • Objects are smart enough to be dynamic. Specifically, they have the ability to react differently depending on the situation. 
  • This is important because it makes the API dependable and easy to understand for the incoming developer. 
  • Let's look at an example. Suppose you have a method called "print()". 
  • The first question you might ask is, "what does this method print: pictures, text, or what?" 
  • Well using "polymorphism", an object can be made to handle any scenario with the exact same method name. Thus, depending on what the object is asked to print, it will be able to print it. 
  • "Polymorphism, is Greek for 'many shapes' and it refers to the ability of an object to have numerous methods with the same name. This is also referred to as method overloading, it occurs in two forms. A subclass can have a method with the same name as found in its superclass. In this case the subclass method overrides the superclass method. Also an object can have multiple methods with the same name but different parameter lists. The appropriate method to execute is choosen at run-time depending on which parameter list is provided in the method call. 
            print(String s)
            print (Graphics g)
            print (char c)
    This is very convenient for the programmer using the object or method because the API is much simplified. Without polymorphism you would have
            printString(Strring s)
            printGraphics(Graphics g)
            printChar(char c)
  • Instead of having separate methods like printPicture(), printLetters(), and printNumbers() you have one unified print() and the object itself determines how to handle the different types of situations. 
One reader, Dave wrote in and said, "Your description of Polymorphism is wrong. I will attempt to clarify. Please excuse the nature of my opening sentence. I hoped to get your attention so I can help (not criticize) to avoid misleading your readers regarding your Java tutorial. 

In a nutshell, polymorphism is bottom-up method calling. Simply put, using your Animal/Mammal/Cat example: 

void test()
{
Cat simon = new Cat();
Animal creature = simon;  // safe upcasting
creature.eat(); // polymorph = Cat.eat()
Dog rover = new Dog();

  // rover safely upcast to Animal reference
  feed(rover);  
  feed(simon);
  }
  void feed(Animal a )
  {
    // creature.eat() = Cat.eat()
    // feed(rover)    = Dog.eat()
    // feed(simon)    = Cat.eat()
    a.eat();
   }
          
Here, it's the Animal.eat() which is polymorphic.

Polymorphism: "Calling a Java/virtual method using a reference to a more generalized superclass of a real object invokes the method in the actual object (the more specific subclass) using a bottom-up searching mechanism".

Your Java Tutorial's definition of polymorphism states that function name overloading is the same thing, which is an incorrect statement."

I have to say that Dave is correct, however, I want to focus on overloading here, so I hope to make both points by adding this note! 

Inheritance
Table of Contents
Exercise Five



Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory