Abstraction and Polymorphism

Recently I was asked the difference between polymorphism and abstraction.

Abstraction and Polymorphism are terms that if not understood in object-oriented programming, will cause nothing but more work and pain when your program goes beyond a few classes or objects.

This explanation will use Java to hopefully better explain the difference.

Polymorphism

This is the ability to work with objects that react differently in a more efficient manner. The different object react differently, yet share the same methods. These methods are found in one common interface.

For example if we were working with different types of ducks, we could say that they all have common things that they do, but do them in slightly different ways.

Duck[] ducks = new Duck[3];

    ducks[0] = new MallardDuck();
    ducks[1] = new RedheadDuck();
    ducks[2] = new RubberDuck();

    for(int i=0; i < ducks.length; i++) {
        System.out.println("Duck #" + (i + 1) + ":\n---------------------");
        ducks[i].quack();
        ducks[i].display();
        System.out.println(ducks[i].toString() + Driver.BLANK_LINE);
    }

OUTPUT:

Duck #1:
---------------------
Quack
Drawing a Mallard Duck to screen
[email protected]

Duck #2:
---------------------
Quack
Drawing a Red Headed Duck to screen
[email protected]

Duck #3:
---------------------
Squeak!
Drawing a Rubber Duck to screen
[email protected]

The use of an interface “Duck” around the types of ducks, allows a programmer to call a for loop in which they can call any method that is contained in the “Duck” interface.

Abstraction

Now where you may be thinking Abstraction and Polymorphism are similar might be in the case where you build your interface. Take the “Duck” interface for example:

    public interface Duck {

    public abstract void display();

    public abstract void performQuack();


}

You can see it is about as abstract as it gets. A true interface does not give any directives. It simply creates a set of common methods that needs to be in each class that implements it. In this way you can have many similar things, that react differently, follow a common “abstract” set of common methods.

It is possible to have a less abstract way to control your ducks. Instead of setting it as an “interface”, you can set it as an “abstract class”.

    public abstract class Duck {

    public abstract void display();

    public abstract void performQuack();

        public void swim() {
        System.out.println("All ducks float, even decoys!");
    }

}

In this class The “swim” method was added. This method contains a static reaction. The “swim” class is ridged. It can be overridden, but none the less, it does not even compare to how abstract the other methods are in this class.

Why is it rigid? The obvious answer would be the String that outputs. Looking even closer, the way it is outputted (to console) makes it even more rigid.

What if your program doesn’t support a console?

This is why Abstraction is important.

I hope this helps to clarify the difference between Polymorphism and Abstraction.

Leave a Reply