Monday, 1 October 2012

Short note on Abstract Class

Java provides a special type of class called an abstract class. Which helps us to organize our classes based on common methods. An abstract class lets you put the common method names in one abstract class without having to write the actual implementation code.
Java Abstract classes are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.

An abstract class can be extended into sub-classes, these sub-classes usually provide implementations for all of the abstract methods.
The key idea with an abstract class is useful when there is common functionality that's  like to implement in a superclass and some behavior is unique to specific classes. So you implement the superclass as an abstract class and define methods that have common subclasses. Then you implement each subclass by extending the abstract class and add the methods unique to the class.

Points of abstract class :

  1. Abstract class contains abstract methods.
  2. Program can't instantiate an abstract class.
  3. Abstract classes contain mixture of non-abstract and abstract methods.
  4. If any class contains abstract methods then it must implements all the abstract methods of the abstract class.
A class abstract Vehicle might be specified as abstract to represent the general abstraction of a vehicle, as creating instances of the class would not be meaningful.
abstract class Vehicle {

int numofGears;
String color;
abstract boolean hasDiskBrake();
abstract int getNoofGears();

No comments:

Post a Comment