Abstract classes are used in Object Oriented Programming languages like Java. An abstract class, in terms of implementation, is somewhere between an implementation class and an interface.

What is an abstract class?

Let us figure out what is an abstract class. Let us assume that you have a base class called BaseClass and a derived class DerivedClass that is the implementation class and is a sub-class of BaseClass.

 

public class BaseClass {
BaseClass(){}
//Default implementation for the method.
//Sub-classes need not override this method
//if they are happy with the default implementation.
public void defaultMethod_1(){
}
//Other default methods go here
//Ideally all sub-classes should have own
//implementations for all the dummy methods
public void dummyMethod_1(){
}
//Other dummy methods go here.
}

 

 


public class DerivedClass extends BaseClass {
//Own implemetation for the dummy method
public void dummyMethod_1() {
}
//Similarly for all the other dummy methods
}
Now say, you want the following
  1. BaseClass needs to have implementation for some of its object methods (let us call them Default methods). So that the DerivedClass need not override them, if they donít want any other behavior.
  2. BaseClass have some of the methods (let us call them Dummy Methods) which, it STRICTLY want all of its child classes to override. Infact, the BaseClass wants to throw an error, if they donít override these methods.
  3. BaseClass is too generic a class (like a Vehicle Class) and it does not make any sense to create an object of it by calling

newBaseClass()
All the initialization should be done through the subclasses. The above implementation of the BaseClass and DerivedClass clearly will not help us in achieving these. One modification to support (1) and (2) may be to throw UnSupportedOperationException() from the dummy methods of BaseClass.

 

//Base class implementation of the dummy methods
public void dummyMethod_1(){
throw new UnsupportedOperationException();
}

 
But they have the following issues.
  1. They will be found only during the run time and not at compile time.
  2. You cannot use UnSupportedOperationException in case of BaseClass constructor to prevent calling the BaseClass constructor directly. Because this constructor gets called even when you are calling a DerivedClass constructor using

BaseClass bc =newDerivedClass();
In fact, it will be the first one to get called.

How to achieve all the three requirements

All the above three requirements can be achieved by modifying the BaseClass to have the dummy methods as abstract methods and make it an abstract class. So here is what you have to do to the BaseClass(you need to modify only the base class
  1. All the dummy methods will have the keyword Ďabstractí. These methods will not have any method bodies but will have only declaration.
  2. If you have more than one abstract methods in a class, then Java does not allow the instantiation of objects of this class. Java achieves this by making sure at compile time that all the classes which have more than one abstract methods, will be abstract classes.

public abstract class BaseClass {
//You can still have an implementation
//for constructors
BaseClass(){
}
//Default implementation for the method.
//Sub-classes need not override this method
//if they are happy with the default implementation.
public void defaultMethod_1(){
}
//Other default methods go here
//Abstract methods have to be implemented by sub-classes
//Notice that they don't have implementation and ends
//with a semicolon.
public abstract void dummyMethod_1();
//Other abstract methods go here.
}
 



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)