Sunday, December 14, 2008

What is Abstraction

Welcome back to Java Code Online. Abstraction is the first Principle of OOPS and one of the core concepts of Java. First of all if you really want to get a hold on the Principle of OOPS and gain a strong understanding on the concepts of Java, then forget everything you have read about Abstraction and start from scratch. Trust me, you won't be disappointed.

Abstraction in laymen term means taking out the common things. First of all concentrate on the word Abstraction, it comes from the word "Abstract". Here you abstract the most common features of an Object and place them in a separate class. I will take some real life examples to make your life more easy.

Suppose that you need to make an Object of Tiger type. I mean to say that, you need a class for making a Tiger. Now just think that, what does a Tiger "do", and what does a Tiger "know". The stress her is on the words "do" and "know", because whatever the Tiger "do" are its methods and whatever the Tiger "knows" are its instance variables.

Now we know that every Tiger has a some basic properties like hunger, location, color, etc. These are the instance variables of Tiger. And every Tiger "does" some basic things like makes noise, eat, sleep, roam around, etc.

Now if we have to make only Tiger Objects then a class for Tiger is ok. But what if we need to add other animals too, like cat, dog, wolf etc. Will you write a seprate class for every animal, knowing that every animal has the same basic properties. Won't it be a waste of energy and time, rewriting the same methods again for every animal.

Here is where Abstraction comes into picture. In abstraction, what you do is take out the common instance variables and methods, so that you need not write them again, you write the basic methods and instance variables in a single abstract class, and then you "extend" other classes from it depending upon their behavior. And just by magic all the methods and instance variables in the abstract class are available to the "extended" classes. The part of extending a class is called Inheritance and is the second Principle of OOPS, I will discuss it in my later posts. For the time being just concentrate on the word Abstraction.

So now for the Tiger class, we make a parent abstract class called Animal. We know that all the animals have more or less the same features. So we write the instance variables like color, hunger, location, etc. into the abstract class. And also we define the methods common to all the animals like makeNoise(), eat(),
roam(), sleep(), etc. and then use these methods and instance variables in the child classes. So now even a wolf, cat and dog can use the code of the abstract animal class, and no need to rewrite the code again for them.

So in technical terms Abstraction is taking out the common code from similar classes and placing it in one particular class, so that it could be used by all the classes using the same functionality. It helps in code reusability, makes the code clearer and better to read and interpret, and most of all makes your life easy.

I hope that I was able to keep my word in explaining you about what is Abstraction. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

Related Articles
Principle of OOPS
Advantage of Abstraction
What is Object


  1. Great post, I love the way you have explained Abstraction. Since my school days this thing was not clear to me, but i really appreciate your way of explaining Abstraction. I have seen your last few posts and really liked them, great going..

  2. You pick the core out of it. Great explanation. Simply love your blog. Do post articles on the Other Principle of OOPS apart from Abstraction


Note: Only a member of this blog may post a comment.