0

My understanding of a Polymorphism is, it is one of the way of achieving Abstraction ? Does every one agree with my view ? Or there is any flaw in my thinking ?

nexusboy
  • 13
  • 3
  • No, abstraction and polymorphism are two separate concepts, both being part of the four tenets of object oriented programming. The other two are encapsulation and inheritance. https://codingarchitect.wordpress.com/2006/09/27/four-tenets-of-oop/ – Enigmativity Nov 25 '17 at 08:10
  • @Enigmativity Hey , My doubt was when we implement an interface or extend an abstract class and derive a subclass/subclasses and when we define the object using the data type of interface / parent class and we call the method of the object. In this case Abstraction of a general set of derived classes is achieved using the concept of Polymorphism right ? – nexusboy Nov 25 '17 at 10:33
  • No, abstraction, polymorphism and inheritance are different things. Did you read the link I gave you? – Enigmativity Nov 25 '17 at 11:01
  • @Enigmativity .. yeah I have read the article which you have shared with me ! I understand that they are different .. So according to you the example I gave uses concepts of both Abstraction and Polymorphism is it ? – nexusboy Nov 25 '17 at 11:05
  • 1
    No, it does not. – Enigmativity Nov 25 '17 at 13:12

1 Answers1

2

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
MallardDuck@15db9742

Duck #2:
---------------------
Quack
Drawing a Red Headed Duck to screen
RedheadDuck@6d06d69c

Duck #3:
---------------------
Squeak!
Drawing a Rubber Duck to screen
RubberDuck@7852e922

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 gets outputted. Looking even closer, the way it get outputted (to console) makes it even more rigid.

What if your program doesn't support a console?

This is why Abstraction is important.

Conclusion

To a slight degree you are on the right track. You do not need to have abstraction to achieve polymorphism. Yet, where your curiosity is heading is correct. Without abstraction there really is no point of making objects polymorphic. If every object does the exact same thing, then those objects do not need to be separate. they just need to be separate instances of the same object.

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

SynchroDynamic
  • 386
  • 2
  • 14
  • Thank you very much , I now completely understand there is a very good difference between abstraction and polymorphism . The doubt which I got was when I was when I was going through the example which you gave in some book . So the conclusion for the example , both abstraction and polymorphism are unique concepts but in the example which we are talking about we are making use of polymorphism on top of abstraction to achieve the functionality which we are getting. (I.e duck array in a for loop) . – nexusboy Nov 26 '17 at 06:21
  • 1
    You are quite welcome. All of these concepts get much easier the more you work with them. As I was writing this answer, I had flashbacks to the lessons I had about these concepts. I can't tell you the number of questions I had about them. I remember thinking to myself, "these concepts are stupid". Today, I couldn't imagine programming without these concepts in my tool belt. Good luck to you in your studies. – SynchroDynamic Nov 26 '17 at 06:32