Java Abstract Class Example
In this tutorial, we will discuss abstraction in java through examples. We are also going to talk about abstract class vs interface implementation in Java.
Data abstraction is the process of hiding certain details and showing only essential information to the user. We can have abstraction with using either abstract classes or interfaces.
An abstract class is a class that is declared using the abstract keyword and it cannot be instantiated. It can be used only as a super-class for those classes that extend theabstract 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, anabstract 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 anabstract method, either declared or inherited, it must be declared as anabstract class.
A class that extends anabstract class must implement all itsabstract methods (if any). Otherwise, the sub-class must be declared asabstract as well. Finally, any implementation of anabstract method can be overridden by additional sub-classes.
You can also check this tutorial in the following video:
The last things to mention are thatabstract classes can alsoimplement methods, despite providing just their signature and that anabstract class may have static fields and static methods.
In this example, we use the following tools on aWindows 7 platform:
- Eclipse Kepler Service Release 1
- Java 1.7.0_51
1. Using a Java Abstract class
The purpose of anabstract 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 anabstract class is the following. We declare an abstract class, calledInstrument:
Instrument.java
1 2 3 4 5 | abstractclassInstrument { protectedString name; abstractpublicvoidplay();} |
As we can observe, anInstrument object contains a field name and a method called play, that must be implemented by a sub-class.
Next, we define a sub-class calledStringedInstrument that extends theInstrument class and adds an extra field callednumberOfStrings:
StringedInstrument.java
1 2 3 | abstractclassStringedInstrumentextendsInstrument { protectedintnumberOfStrings;} |
Finally, we add two more classes that implement the functionality of aStringedIntrument, calledElectricGuitar andElectricBassGuitar accordingly. The definition of these newly added classes is shown below:
ElectricGuitar.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | publicclassElectricGuitarextendsStringedInstrument { publicElectricGuitar() { super(); this.name ="Guitar"; this.numberOfStrings =6; } publicElectricGuitar(intnumberOfStrings) { super(); this.name ="Guitar"; this.numberOfStrings = numberOfStrings; } @Override publicvoidplay() { System.out.println("An electric "+ numberOfStrings +"-string "+ name +" is rocking!"); }} |
ElectricBassGuitar.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | publicclassElectricBassGuitarextendsStringedInstrument { publicElectricBassGuitar() { super(); this.name ="Bass Guitar"; this.numberOfStrings =4; } publicElectricBassGuitar(intnumberOfStrings) { super(); this.name ="Bass Guitar"; this.numberOfStrings = numberOfStrings; } @Override publicvoidplay() { System.out.println("An electric "+ numberOfStrings +"-string "+ name +" is rocking!"); }} |
Finally, we create a new class calledExecution that contains a single main method:
Execution.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | importmain.java.music.ElectricBassGuitar;importmain.java.music.ElectricGuitar;publicclassExecution { publicstaticvoidmain(String[] args) { ElectricGuitar guitar =newElectricGuitar(); ElectricBassGuitar bassGuitar =newElectricBassGuitar(); guitar.play(); bassGuitar.play(); guitar =newElectricGuitar(7); bassGuitar =newElectricBassGuitar(5); guitar.play(); bassGuitar.play(); }} |
In this example, we create two different instances of anElectricGuitar and anElectricBassGuitar 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!
1.1 The Purpose of Abstract Classes
The purpose ofabstract classes is to function as base classes which can be extended by subclasses to create a full implementation.
Due to this feature, an Abstract Class is being used in scenarios where a complete implementation can be further broken down into a set of repetitive steps with few unique steps.
One such example will be, ofURL Processor application which extract the HTML of the website hosted at the URL.
URLProcessorBase.java
package URLProcessor;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;public abstract class URLProcessorBase { public void process(URL url) throws IOException { URLConnection urlConnection = url.openConnection(); InputStream input = urlConnection.getInputStream(); try { processURLData(input); } finally { input.close(); } } protected abstract void processURLData(InputStream input) throws IOException;}This is a base class that contains the methods used for the implementation of the URL processor.
Now we can have the multiple subclasses of this base class based on the kind of processing that needs to be done on the website hosted at the URL provided as input.
In our example code, we are extracting the HTML passed as the response when we call the URL.
URLProcessorImpl.java
package URLProcessor;import java.io.IOException;import java.io.InputStream;public class URLProcessorImpl extends URLProcessorBase { @Override protected void processURLData(InputStream input) throws IOException { int data = input.read(); while (data != -1) { System.out.print((char) data); data = input.read(); } }}Now the main driver code.
Driver.java
package URLProcessor;import java.io.IOException;import java.net.URL;public class Driver { public static void main(String[] args) throws IOException { System.out.println("Driver Code for URL Processor Application using Abstract Class Method"); System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); System.out.println(); URLProcessorImpl urlProcessor = new URLProcessorImpl(); urlProcessor.process(new URL("https://abhiit89.github.io/")); System.out.println(); System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); }}The output of the driver program is shown below.

If we apply a new implementation of theURLProcessorBase class, as shown below.
URLProcessorImplToExtractDataLength.java
package URLProcessor;import java.io.IOException;import java.io.InputStream;public class URLProcessorImplToExtractDataLength extends URLProcessorBase { @Override protected void processURLData(InputStream input) throws IOException { byte[] data = input.readAllBytes(); System.out.print(data.length); }}URLProcessorImplToExtractDataLength.java is another implementation of the base classURLProcessorBase.java which calculates the length alone of the retrieved data. This implementation in a way enforces the SRP (Single Responsibility Principle).
We can override the original driver class to include this new implementation, but for the sake of clarity, a new driver class called theLengthDriver.java is created.
LengthDriver.java
package URLProcessor;import java.io.IOException;import java.net.URL;public class LengthDriver { public static void main(String[] args) throws IOException { System.out.println("Driver Code for URL Processor Application to Extract length of Retrieved Data using Abstract Class Method"); System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); System.out.println(); URLProcessorImplToExtractDataLength metaProcessor = new URLProcessorImplToExtractDataLength(); metaProcessor.process(new URL("https://abhiit89.github.io/")); System.out.println(); System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); }}The output of LengthDriver.java is shown in the snapshot below.

This covers the examples related to the Abstract Class.
2. Java Abstract Class vs Interface
In this section, we are going to talk about abstract class vs interface implementation. Java provides and supports the creation of abstract classes and interfaces.
As we stated at the beginning, we can achieve the abstraction by using eitherabstract classes orinterfaces. 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 subclass 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 the main method.
You can learn more about the abstract class vs interface implementation by readingthis example.
3. Download the Eclipse Project
That was a tutorial about the JavaAbstract Class.
The Eclipse project of the Java Abstract Class Example:Java Abstract Class Example
Last updated on Mar. 18th, 2020

Thank you!
We will contact you soon.




