I seem to not understand two OOP concepts very well. Could you explain what abstraction and polymorphism are, preferably with real examples and
short answer: abstraction is conceptual, polymorphism is behavioral
In simple term, Abstraction is conceptual and Poly is behavioral. In order to achieve abstraction in OOP, you need Poly.
Abstraction in object oriented programming is a concept or design pattern I may say, which enables better isolation, loosely coupling thus testability, and reusability and extensibility. In order to achieve all, we need poly, inheritance/extension and etc.
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.
Imagine a pen and two derived classes:
class pen:
void draw(int x, int y)
class pen_thin extends pen:
void draw(int x, int y) { color(x, y) = green; }
class pen_thick extends pen:
void draw(int x, int y) { color(x, y) = green;
color(x, y+1) = green; }
and two objects:
pen_thin(p1)
pen_thick(p2)
Both pens can draw. your general "pen" cannot draw itself. It's just an interface to pen_thin, pen_thick and lots of other pens. You say: obj1.draw(1, 0); and whether obj1 is a thick or a thin pen doesn't matter to you as a user, neither to the compiler at compile time. The call behaves polymorphic. It's dynamic polymorphism (happens at runtime) and that's what people usually mean. Static Polymorphism happens at compile time:
class colorizer:
void colorize(shirt s)
void colorize(pants p)
That's called overloading. You call obj.colorize(something)
. If you call it with a shirt reference, it will call the version taking a shirt. And if you call it with a pant reference, it will call the pants version. The choice done here is at compile-time.
Abstraction and polymorphism are critical concepts by no means limited to OO. Adding to the confusion, the word 'abstraction' is used multiple ways. Here is a quick cheat sheet with one example:
Data abstraction means information hiding. Usually what is hidden is the representation of a data structure. Example: I implement sets, but I don't tell you whether a set is represented as a list, a balanced binary tree, or an unbalanced binary tree. Done right, I can change representation without breaking your code.
Polymorphism means reuse with different types. So with my set example you could create sets of Social Security numbers, sets of full names, or sets of fruitbats, all using the same code.
Obviously you can define a class which is both abstract and polymorphic.
Polymorphism is further confusing because there are two ways to implement polymorphism. In parametric polymorphism, you can reuse the set with values of any type, or maybe any type satisfying some constraint. The most obvious examples are C++ templates; if you write
class Set <T> { ... }
Then T
is the type of objects contained in the set (the notation <T>
indicates a so-called "type parameter", which is what makes it parametric polymorphism).
In subtype polymorphism, you can reuse sets only with objects whose types are subtypes of a particular type. For example, you might be able to make sets only of objects that offer a less-than-or-equal-to method. In a true object-oriented language like Smalltalk or Ruby, which offer so-called duck typing (us pointy-headed theorists sometimes call it behavioral subtyping), the presence of the method is good enough. In a language like Java or C++, which conflate subtyping with inheritance, your use of polymorphism may be restricted to subclasses of a particular class. (Java further confuses the issue by using one form of subtyping on classes and another on interfaces.)
Finally, old farts like me talk about procedural abstraction, which just means being able to take a bunch of statements that are frequently used together and plop them into a procedure or method which you can then reuse. It's probably not germane to your question.
So, do you feel better about being confused?
P.S: recently started learning java answer is based on my observation please correct me if i am wrong.
Abstraction and Polymorphism basically deep down does almost same work in programming .
let's Take a car for example..
it doesn't matter whether it is a Ford mini-van, Ferrari exotic, Land-Rover SUV or a BMW sedan they all follow a basic design of a car i.e an engine, a steering wheel , a gear box , lights , indicators and list goes on . what makes them different is their specific implementations like Ferrari might have a more powerful engine than a mini van, a suv might have a different gear box hence A car (Superclass over here) has been implemented by subclasses (sedan, suv , mini-van, exotic) This is polymorphism, a basic idea being inherited or implemented by adding other specification. a 4 wheel vehicle (superclass) being implemented in various forms (subclasses)
Now, Abstraction , by definition it means hiding the details and making user see what is required by him..
Let's take example of car again.. You use gear but you don't know exactly the mechanism of how exactly the gear works and changes speed and all..
Now over to coding Part.
Abstract classes are incomplete classes and for a class to be abstract as the name suggest they need to have an incomplete method that needs to be completed by the subclass inheriting the superclass , IF they don't complete the abstract method they will stay incomplete as well.
abstract class car {
abstract void gear();
}
class sedan extends car {
public void gear()
{
//complete the method
}
}
also you can not create objects of abstract classes because class is not complete. Yet these abstract classes can have static methods , arguments , concrete methods BUT for them to be abstract they need one abstract method. So one basic abstract superclass is implemented in other subclasses where they complete it By looking at the method declaration we can estimate what exactly the method is doing, what it is going to return. But we don't know how exactly the abstract method will be implemented.
By using abstract classes or interfaces , we can achieve abstraction in Java. As we all know that abstract classes, interfaces contains abstract methods
We can only estimate how they will work . We get to know how they work, once we provided the method implementation in the classes which implement the corresponding abstract class or interface.
HENCE, abstract basically helps polymorphism.