Tuesday, July 24, 2007

Abstract Classes...

Last week we have learned about Abstract Classes and Interfaces in our JPDC-2007 class.Here I am trying to write some features about Abstract Classes.

Abstract Classes are the classes which are declared abstract.It may or may not contain abstract methods. Abstract methods are methods which do not have method body.Any method can be made abstract by writing access specifier i.e public, private, protected or default, followed by keyword "abstract", followed by return type and method name which is followed by semicolon.It does not contain method body.

If a subclass extends the abstract class, it must contain the implementations for all the abstract methods declared in its parent class.If it does not implement any abstract method declared in its parent class, the subclass itself must be declared as abstract.

Another feature of Abstract class is it cannot be instantiated.It means we cannot create object of Abstract class.The reason why we cannot create object of Abstract class is Abstract class can have abstract methods. And Java does not allow us create object of any class which contains methods that does not have method body.

There may be situations where we have abstract class that does not contain any abstract methods. So question arises what is the use of such class.The answer is simple.We want that no user will be able to create object of our Abstract class directly.If he wants to use our abstract class he needs to extend our class and implement the abstract methods declared in parent class in its own way according to their requirement.

So that's just an introduction about Abstract classes.I am concluding this post with hope that this post might be helpful to anyone reading this blog.

Regards,
Tausif Khan, Nagpur

No comments: