How to Explain Polymorphism to your Mom

Polymorphism

 

Polymorphism provides one of the most powerful programming techniques of the object-oriented paradigm. 

Generally speaking, polymorphism is the ability to appear in many forms. The word polymorphism is derived from two Greek words poly which means many and morphos which means forms. Thus, polymorphism is defined as one thing in many forms. Polymorphism is the ability of an object to take on many forms. In software development, it is the ability to treat an object of any subclass of a base class as if it were an object of the base class. A base class has, therefore, many forms: the base class itself, and any of its subclasses.

When applied to object oriented programming languages like Java, it describes a language’s ability to process objects of various types and classes through a single, uniform interface.

Polymorphism is considered to be a requirement of any true object-oriented programming language (OOPL).

Polymorphism includes the ability to define multiple classes with functionally different, yet identically named methods or properties that can be used interchangeably by client code at run time.

More specifically, polymorphism is the ability to redefine methods for derived classes. For example, given a base class Animal, polymorphism enables the programmer to define different kinds of Animal methods for any number of derived classes, such as sounds they make, food they eat, what they do. No matter what kind of Animal an object is, applying the Sound method to it will return the correct results.

Runtime polymorphism is essentially referred to as method overriding. Method overriding is when you implement inheritance in your program.

An important example of polymorphism is how a parent class refers to a child class object. In fact, any object that satisfies more than one IS-A relationship is polymorphic in nature.

In Java, all Java objects are polymorphic, since any object will pass the IS-A test for their own type and for the class Object. 

As a simple real world example: An application can have Animal class, and its specialized sub classes like Cat and Dog. These subclasses will override the default behavior provided by Animal class + some of its own specific behavior.

For instance, lets consider a class named Animal and let Dog be a subclass of Animal. So, any dog "IS A" Animal. Here, Dog satisfies the IS-A relationship for its own type as well as its super class Animal.

In the following example we make cats and dogs subtypes of animals. The procedure letsHear() accepts an animal, but will also work correctly if a subtype is passed to it:

abstract class Animal {

    abstract String talk();

}

 

class Cat extends Animal {

    String talk() {

        return "Meow!";

    }

}

 

class Dog extends Animal {

    String talk() {

        return "Woof!";

    }

}

 

void letsHear(Animal a) {

    println(a.talk());

}

 

void main() {

    letsHear(new Cat());

    letsHear(new Dog());

}

 

The underlying mechanism that makes polymorphism possible is dynamic binding. 

To see how to use polymorphism in a Java program, consider the family of types. To use an object of type Animal, you must create an Animal object with new and store the returned reference in a variable:

 Animal myFavoritePet = new Animal();

The myFavoritePet variable holds a reference to an Animal object. This is a sensible arrangement; however, there is another possibility brought to you courtesy of polymorphism. Because of polymorphism, you can assign a reference to any object that "is a" Animal to a variable of type Animal. 

Animal myFavoritePet = new Dog();

// or...

Animal myFavoritePet = new Cat();

Therefore, you can sprinkle some polymorphism into your Java program simply by using a variable with a base type to hold a reference to an object of a derived type.

To get the full benefit of polymorphism in your programs, however, you'll need to go further. To fully realize the wonders of polymorphism, you must send a message to an object without knowing the actual class of the object. To do this in Java, you just invoke a method defined in a base type on an object referenced by a variable of the base type. 

Sharing is caring:

Dialogue & Discussion