Thursday, December 18, 2008

What is an abstract Class

Welcome back to Java Code Online. This is one of the most commonly asked interview questions. Now that we have learned What is Abstraction and What is a Class, we are ready to learn what is an abstract class in Java. If anyone is not clear about abstraction, then I do insist that first read my previous post of What is Abstraction. This will help you in a better understanding of this concept.

An abstract class in Java is one which cannot be instantiated. I know that this was bouncer for you, but this is the actual definition of abstract class. I will explain you what I mean by this. In my last article on What is Abstraction, I discussed about the Tiger Object and the abstract class called Animal. So now we are aware that the animal class is an abstract class.

We know that we can create new Objects in Java using the "new" operator. So what will happen, when someone write the code as:-

Animal anim = new Animal();

This is the code for instantiating an Animal class, but wait, you can't define an Animal Object. It is too abstract to define it. What color it would be, what size it would be, what shape it would be. There are so many questions for that. Infact the abstract classes should never be instantiated. You can do this by writing the keyword "abstract" before the access modifier of the class. I will show you how:-

abstract public class Animal
{
public void color()
{
// Your code here.
}
}

Now the above class is declared as abstract. So whenever anyone tries to make a new Object of type Animal by saying that:-

Animal anim = new Animal();

The compiler will complain with the error:-

Animal is abstract; cannot be instantiated
Animal anim = new Animal();
1 error


If you don't want your compiler to complain, you better take care of this point.

Abstract classes are practically useless if not extended. By this I mean that, if you cannot create an Object of an abstract class in Java, what good it would be for you. You cannot perform operation on the class, infact you can do nothing on that class. An abstract class is only helpful, if it is extended. Here I am taking about Inheritance, which is another strong Principle of OOPS. Actually all the four Principle of OOPS abstraction, encapsulation, inheritance and polymorphism work together in making Java as a very powerful tool for development of applications.

So I was taking about extending an abstract class. When you extend an Abstract class, you inherit all the methods and instance variable defined in the abstract class, then on that you can perform your operations. I will discuss more on this in my article on Inheritance. For the time being just concentrate on the abstract class part.

So now you are aware that an abstract class should not be instantiated, and you can avoid it by using the keyword "abstract". If you like this article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

No comments:

Post a Comment

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