Abstract class is an important concept of Object Oriented Programming (OOP) in general and Java in specific. What is abstract class? Why using abstract class? When using abstract classes? You will find my answers in this article, with explanatory code examples.

 

1. What is Abstract Class in Java?

In Java, a class is abstract when it has one or more methods that are declared abstract, which do not have concrete implementation (methods have no body). In other words, an abstract class is incomplete: it is partially implemented with methods that have body, and leaving no-body methods to be implemented by subclasses.

Let’s see an example which is described in the following class diagram:

Java abstract class diagram

In this example, Animal is an abstract class that has two concrete methods eat() and sleep(), and one abstract method move():

package net.codejava.core;

public abstract class Animal {

	public void eat() {
		System.out.println("eating...");
	}

	public void sleep() {
		System.out.println("sleeping...");
	}
	
	public abstract void move();
}
This means subtypes of animal will inherit the behaviors eat and sleep, and develop the move behavior specifically to each concrete type. For example, below is code of the class Eagle:

package net.codejava.core;

public class Eagle extends Animal {

	@Override
	public void move() {
		System.out.println("I'm flying on the sky");
	}
}
You see, this means the move behavior of an eagle is flying, which is specific to its type. Similarly, below is code of the subclasses Shark and Alligator:

public class Shark extends Animal {

	@Override
	public void move() {
		System.out.println("I'm swimming under the sea");
	}

}


public class Alligator extends Animal {

	@Override
	public void move() {
		System.out.println("I can swim in water and crawl on land");
	}

}
So, an abstract class has at least one abstract method and it is declared with the abstract modifier. An abstract class can provide implementation for some methods which will be reused (inherited) by its subclasses. And it also defines some abstract methods that must be implemented by its subtypes.



And the following small program helps you understand more about how abstract class is used:

package net.codejava.core;

public class AnimalTest {

	public static void playWithAnimal(Animal animal) {
		animal.move();
		animal.eat();
		animal.sleep();
	}
	
	public static void main(String[] args) {
		Animal eagle = new Eagle();
		Animal shark = new Shark();

		playWithAnimal(eagle);
		
		playWithAnimal(shark);
		
		playWithAnimal(new Alligator());
	}

}
Run this program and you will see the following output:

I'm flying on the sky
eating...
sleeping...
I'm swimming under the sea
eating...
sleeping...
I can swim in water and crawl on land
eating...
sleeping...
The method playWithAnimal() takes a generic parameter of type Animal - it calls all methods (behaviors) of Animal without caring about the actual object type. And depending on the actual type of passed object (Shark, Eagle or Alligator), the move operation will behave differently.


2. When Using Abstract Classes?

Consider using abstract classes when some code can be reused via inheritance (subclasses inherit methods implemented in the abstract super class), and some behaviors can be dynamic (subclasses implement abstract methods defined in the super class). In other words, using abstract classes help producing reusable and flexible code. It also means you can use abstract classes to structure the code in a better-organized way.

Also consider using abstract classes when you want to force users of your code must provide required implementation code because an abstract class cannot be instantiated (you can’t create new objects from an abstract class).

A good example of using abstract classes is in the JDK’s Collections framework. Let’s look that the following class diagram that depicts a portion of the List collection API:

List class diagram

The abstract class AbstractCollection implements some common operations for collections (isEmpty(), contains(), toArray()…) and define some operations that need to be implemented by more specific subtypes (iterator(), size()…).

The AbstractList class inherits AbstractCollection and it provides implementation for iterator() and size().

ArrayList and Vector are two subtypes of AbstractList so they inherit all operations that are common to a List, plus each class implements behaviors specific to its own type. With this design, when you want to create your own type of List, you can just inherit the AbstractList class and implements the specific part in your class.


3. Some Rules about Java Abstract Class

There are some rules which you need to know with regard to abstract classes in Java. Below are the main points:

  • If a class has an abstract method, it must be declared as abstract. But an abstract class can have no abstract methods at all.
  • You can’t create new objects from an abstract class. Abstract classes are incomplete so they can’t be instantiated.
  • The first non-abstract class must provide implementation for the abstract methods declared in its super abstract class.
  • An abstract class can inherit another abstract class. In this case, the subclass is free to implement the abstract methods defined in the superclass or not.
That’s my article about abstract class in Java - what it is, when it is used and some rules about Java abstract classes. I recommend you to learn more below.

Watch the following video to see the coding in action:

 

Further Reading:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment