Home » Java Basics » Java Abstract Class Example

About Sotirios-Efstathios Maneas

Sotirios-Efstathios Maneas
Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.

Java Abstract Class Example

In this tutorial we will discuss about the Abstract class in Java. An abstract class is a class that is declared using the abstract keyword. An abstract class cannot be instantiated. It can be used only as a super-class for those classes that extend the abstract class. The default functionality of the class still exists, with its fields, methods and constructors being accessed in the same way as with the other classes.

Moreover, an abstract class may contain methods without any implementation, called abstract methods. The declaration of an abstract method starts with the abstract keyword and ends with a semicolon, instead of the method’s body. If a class contains an abstract method, either declared or inherited, it must be declared as an abstract class.

A class that extends an abstract class must implement all its abstract methods (if any). Otherwise, the sub-class must be declared as abstract as well. Finally, any implementation of an abstract method can be overridden by additional sub-classes.

Last things to mention are that abstract classes can also implement methods, despite providing just their signature and that an abstract class may have static fields and static methods.

In this example, we use the following tools on a Windows 7 platform:

  • Eclipse Kepler Service Release 1
  • Java 1.7.0_51

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Using an Abstract class

The purpose of an abstract class is to specify the default functionality of an object and let its sub-classes to explicitly implement that functionality. Thus, it stands as an abstraction layer that must be extended and implemented by the corresponding sub-classes.

A sample example of using an abstract class is the following. We declare an abstract class, called Instrument:

Instrument.java:

abstract class Instrument {
     protected String name;

     abstract public void play();
}

As we can observe, an Instrument object contains a field name and a method called play, that must be implemented by a sub-class.

Next, we define a sub-class called StringedInstrument that extends the Instrument class and adds an extra field called numberOfStrings:

StringedInstrument.java:

abstract class StringedInstrument extends Instrument {
     protected int numberOfStrings;
}

Finally, we add two more classes that implement the functionality of a StringedIntrument, called ElectricGuitar and ElectricBassGuitar accordingly. The definition of these newly added classes is shown below:

ElectricGuitar.java:

public class ElectricGuitar extends StringedInstrument {

     public ElectricGuitar() {
          super();
          this.name = "Guitar";
          this.numberOfStrings = 6;
     }

     public ElectricGuitar(int numberOfStrings) {
          super();
          this.name = "Guitar";
          this.numberOfStrings = numberOfStrings;
     }

     @Override
     public void play() {
          System.out.println("An electric " + numberOfStrings + "-string " + name
				+ " is rocking!");
     }
}

ElectricBassGuitar.java:

public class ElectricBassGuitar extends StringedInstrument {

     public ElectricBassGuitar() {
          super();
          this.name = "Bass Guitar";
          this.numberOfStrings = 4;
     }

     public ElectricBassGuitar(int numberOfStrings) {
          super();
          this.name = "Bass Guitar";
          this.numberOfStrings = numberOfStrings;
     }

     @Override
     public void play() {
          System.out.println("An electric " + numberOfStrings + "-string " + name
				+ " is rocking!");
     }
}

Finally, we create a new class called Execution that contains a single main method:

Execution.java:

import main.java.music.ElectricBassGuitar;
import main.java.music.ElectricGuitar;

public class Execution {

     public static void main(String[] args) {
          ElectricGuitar guitar = new ElectricGuitar();
          ElectricBassGuitar bassGuitar = new ElectricBassGuitar();
          
          guitar.play();
          bassGuitar.play();

          guitar = new ElectricGuitar(7);
          bassGuitar = new ElectricBassGuitar(5);

          guitar.play();
          bassGuitar.play();
     }
}

In this example, we create two different instances of an ElectricGuitar and an ElectricBassGuitar classes and we call their play methods. A sample execution of the aforementioned main method is shown below:

An electric 6-string Guitar is rocking!
An electric 4-string Bass Guitar is rocking!
An electric 7-string Guitar is rocking!
An electric 5-string Bass Guitar is rocking!

Abstract Class vs. Interface

Java provides and supports the creation of abstract classes and interfaces. Both implementations share some common features, but they differ in the following features:

  • All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
  • A class may implement a number of Interfaces, but can extend only one abstract class.
  • In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
  • Abstract classes can implement interfaces without even providing the implementation of interface methods.
  • Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
  • Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
  • An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.

Download The Eclipse Project

The Eclipse project of this example: AbstractClassExample.zip.

 
This was a tutorial about Java’s abstract class.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns