Abstraction 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.

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:

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 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 a main method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s