Home > Java > Polymorphism > Introduction
Introduction

Introduction to Polymorphism

Polymorphism is the third essential feature of an object-oriented programming language, after data abstraction and inheritance.


In Java the term polymorphism (from the Greek, meaning “many forms”) means “a method the same as another in signature (name, parameter list, return type) but with different behavior.”


The Java engine is able to dynamically, at run time, choose one of several method definitions to execute for a single method call. This capability is called polymorphism.


Polymorphism is a way of giving a method one name that is shared up and down an object hierarchy, with each object in the hierarchy implementing the method in a way appropriate to itself.


Syntax

 

class Animal {

    public void makeNoise() {}

}

class Dog extends Animal {

    public void makeNoise() {
        System.out.println(“Bark! Bark!”);
    }

}

class Cat extends Animal {

    public void makeNoise() {
        System.out.println(“Moo! Moo!”);
    }
}


Example


1.  /**
2.  * @(#)PolymorphismDemo.java
3.  *
4.  *
5.  * @author
6.  * @version 1.00 2009/11/21
7. */
8.
9.  class Animal {
10.    
11.    String name;
12.    int age;
13.    
14.    Animal(String animalName, int animalAge) {
15.        
16.        name = animalName;
17.        age = animalAge;
18.        
19.    }
20.    
21.    public void makeNoise() { }
22.        
23. }
24.
25. class Dog extends Animal {
26.    
27.    Dog(String name, int age) {
28.        
29.        super(name,age);
30.    }
31.    
32.    public void makeNoise() {
33.        
34.        System.out.println("Dog is making noise Bark! Bark!");
35.    }
36.    
37.  }
38.
39.  class Cat extends Animal {
40.    
41.    Cat(String name, int age) {
42.        
43.        super(name,age);
44.    }
45.    
46.    public void makeNoise() {
47.        
48.        System.out.println("Cat is making noise Moo! Moo!");
49.    }
50.    
51.  }
52.
53.  public class PolymorphismDemo {
54.        
55.    /**
56.     * Creates a new instance of <code>PolymorphismDemo</code>.
57.     */
58.    public PolymorphismDemo() {
59.    }
60.    
61.    public void getNoise(Animal animal) {
62.       
63.        animal.makeNoise();    
64.        
65.    }
66.    
67.    /**
68.     * @param args the command line arguments
69.     */
70.    public static void main(String[] args) {
71.        // TODO code application logic here
72.        
73.        PolymorphismDemo obj = new PolymorphismDemo();
74.        
75.        Dog dog = new Dog("Dillo", 2);
76.        
77.        Cat cat  = new Cat("Moona", 1);
78.        
79.        obj.getNoise(dog);
80.        obj.getNoise(cat);
81.   }
82.  }

 

Output


Dog is making noise Bark! Bark!
Cat is making noise Moo! Moo!


Dog and Cat classes have same method name i.e. makeNoise but different behavior. It’s an example of polymorphism.  


On line numbers 83 and 84, the method getNoise is called and dog or cat reference is passed to it. The dog or cat reference is assigned to animal object of Animal class. The Animal class is parent of Dog and Cat class. This is upcasting.


How would the compiler know which makeNoise method to use for either a dog or a cat? The answer is that the compiler doesn’t know, and the decision on which makeNoise method to use is postponed until run-time! Dynamic method lookup is a technique where each object has a table of its methods, and Java searches for the correct versions of any overridden methods at run-time.

Leave a Reply

Fields with * are mandatory.
* Name:
Website:
* Comments:
 
Tags allowed: <strong> <i> <u>
* Code:
 
 
Submit   Cancel