What Is the Difference Between Abstraction and Polymorphism

What Is the Difference Between Abstraction and Polymorphism

I seem to not understand two OOP concepts very well. Could you explain what abstraction and polymorphism are, preferably with real examples and code?

Thank you.

2

11 Answers

Abstraction

Imagine a fraction class:

class fraction:
    int denominator
    int numerator

Now two objects of that:

fraction(obj1): denominator=-1 numerator=-1
fraction(obj2): denominator=1  numerator=1

Both objects have the value 1: (1/1) == (-1)/(-1). You wouldn't expect they behave any different to the outside. That's abstraction. You abstract the data your object holds into a logical view, even tho behind the scenes, there are other things. Theoretically, you have got a equivalence relation, with different equivalence groups:

[1]=(1, 1), (-1, -1), (5, 5), ...
[2]=(2, 4), (-2, -4), ...
...

And there is a abstraction function that abstracts the internal details to the outside:

f((1, 1)) = [1]
f((-1, -1)) = [1]

It maps from concrete values to the abstract values of an object. You do that by writing for example a constructor mapping (-1, -1) to (1, 1) and by writing a equals function for your class.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.