blog

Home / DeveloperSection / Blogs / Abstract Classes in Java

Abstract Classes in Java

David Miller 1334 19-May-2016

For an interface containing several methods, a developer can provide the implementation for some of the methods in a class that implements the interface. However, the developer may not be in a position to implement all the methods of the interface and might leave that task to a colleague or senior developer to perform at a later time. In such a case, compiling the class would result in compile-time errors. This situation can be remedied by declaring the class as abstract, and we create an abstract class using the abstract keyword.

Let’s consider the case where a developer implementing our HybridVehicle class does not know the implementation of the getMilesPerGallon method, which requires a few computations that may not be known at the time of code development. In such a situation, the developer can provide the implementation of all other methods of the two interfaces, except for the getMilesPerGallon method. The class HybridVehicle must now be declared abstract, as follows:

abstract class HybridVehicle                                           

implements ExtendedMileageEfficiency, BatteryLifeTracker {

The code will now compile; however, the developer will not be able to create an instance of HybridVehicle anywhere in the program. To create an instance of HybridVehicle, he will probably extend this class further and provide the implementation of the getMilesPerGallon method in the new class.

A typical use of abstract classes is seen in our earlier example of the Modem interface. In the Modem interface, we declared four methods: open, close, read, and write. We could add one more method called init to this interface. The purpose of the init method, as the name suggests, is to initialize the modem. The new interface is shown in the following code snippet:

interface Modem {
public boolean open();
public boolean close();
public int read ();
public int write(byte[] buffer);
public void init();
}

 Because the implementation of the read and write methods is mostly the same for all the modems, we can provide these implementations for the benefit of modem manufacturers. However, the implementation of the open, close, and init methods will differ for each manufacturer. In particular, the init method that initializes the modem hardware will surely vary from manufacturer to manufacturer. Therefore, we may create a new class, AbstractModem, that provides the implementation of the Modem interface except for the implementations of open, close, and init methods. This class must be declared abstract because it does not provide the implementations of all the methods of the implementing interface. The class definition is shown here:

abstract class AbstractModem implements Modem {

                     public int read() {
                             int bytesRead = 0;
                             // some implementation
                             return bytesRead;
                     }
                     public int write(String buffer) {
                             int bytesWritten = 0;
                             // some implementation
                             return bytesWritten;
                     }
}

 Thus, the abstract classes allow us to provide the partial implementation of the implementing interface and leave the rest of the implementation to another developer. Because the abstract class has some missing implementation, an abstract class cannot be instantiated. The following statement would generate a compile-time error:

Modem modem = new AbstractModem(); // generates compile-time error

I.e. Abstract classes cannot be instantiated, but they can be sub-classed.
Differences between an interface and an abstract class:
  •  An interface contains only the method signatures whereas an abstract class may have some of its methods implemented.
  •  All of an interfaces methods are public by default. You cannot apply any other access modifiers to the methods declared in an interface. In an abstract class, the implemented methods can have access modifiers applied to them in their declarations. For this, the methods have to be public in an interface. Declaring them protected or private would result in an error. In an abstract class, you can apply a protected modifier to an implemented method but you cannot make it private.
  •  An interface can extend multiple interfaces. An abstract class cannot be extended from more than one abstract class.
  •  All methods in an interface are implicitly abstract. An abstract class may have a few concrete methods.
  •   An interface does not have a constructor. An abstract class may declare a constructor.

Updated 15-Mar-2018

Leave Comment

Comments

Liked By