Abstraction

Abstraction is a way of dealing with things in a general way as much as possible, separate from specific details, implementations and instances.

According to wikipedia:

Abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time.

In this blog post, I will show to you simple ways how to achieve abstraction. Some of you implemented this concept but you’re unaware of it.

Abstraction via Functions

  • Processes can be grouped into functions
  • One function can call other function
  • You don’t have to understand each line, just what the function does
  • You can hide data to be accessible only within the function – local variables

double areaOfCircle(int radius) {

	return PI * radius * radius;

}

boolean isFirstBiggerThanSecond(int radius1, int radius2) {
	double area1, area2;
	area1 = areaOfCircle(radius1);
	area2 = areaOfCircle(radius2);
	return area1 > area2;
}

The areaOfCircle() method computes the area of the circle. The isFirstBiggerThanSecond() method compares the size of the circle if the first circle is bigger than the second one given the radius of the two. The isFirstBiggerThanSecond() called the areaOfCircle(). The one who writes the code of the isFirstBiggerThanSecond() method does not need to know how the implementation of the areaOfCircle() method. What he need to do is just to know what the areaOfCircle() does. The implementation on how to compute the area of the circle is separated and factored out so that who will ever use that method does not need to know how the computation is done, only what the function does.

Abstraction via Objects

Combining complex data types and the functions that operates on them give you objects.

public class Rectangle {
    private int length;
    private int width;

    public Rectangle (int length, int width) {
        this.length = length;
        this.width width;
    }

    public int area() {
        return length * width;
    }

    public boolean isBiggerThan(Rectangle other) {
        return this.area() > other.area();
    }   
}

Rectangle rect1 = new Rectangle(3, 4);
Rectangle rect2 = new Rectangle(5, 6);

if (rect1.isBiggerThan(rect2)) {
    buyPaint(rect2.area());
} else {
    buyPaint(rect1.area());
}

Objects are mini-programs, each with its own data and operations.

In Unix, we have a lot of little programs (grep, ls, cat…) that do just one thing. We can combine the little programs into bigger programs by having the output of one become the input of another.

OOP is the same. We make small, simple, specialized programs, and then make bigger programs by combining little programs (“Composition”).

“Think of an OO system as a bunch of intelligent animals (the objects) inside your machine, talking to each other by sending messages to one another.” – Allen Holub

OOP is a way to make programs easier to understand, by allowing bigger programs to be composed of smaller programs, and those smaller programs to be composed of even smaller programs, ad infinitum.

Those are only simple concepts that relates to abstraction. I found an interesting article which discusses abstraction using interface and abstract class in java.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s