Polymorphism vs Abstraction : Is polymorphism one of the way to achieve Abstraction?

你。 提交于 2019-12-11 15:07:42

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/47483673/polymorphism-vs-abstraction-is-polymorphism-one-of-the-way-to-achieve-abstract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!