How to Write an Abstract Class in Java

How to Write an Abstract Class in Java

In Java, abstract classes allow you to define classes that are not actually instantiated themselves, but other classes can extend. These other classes which inherit from the abstract classes can be instantiated as objects within an application. Abstract classes therefore have a similar function to interfaces in Java, allowing developers to specify the behavior and data inheriting classes must implement. Declaring an abstract class is a straightforward process, requiring only a few simple Java code elements.

Instructions

    1

    Create an outline for your abstract class declaration. Create a new class in your Java application project and choose a name to suit its purpose, such as "AbstractDemo," for example. Enter the following outline code, changing the class name to suit your own:

    public abstract class AbstractDemo

    //class code here

    The specifics of the abstract class will be listed within this class declaration. The keyword "abstract" specifies this is a class which will not be instantiated.

    2

    Add data fields to your abstract class declaration. Abstract class declarations can determine variables which will be reflected in any subclasses. Add a simple data item as follows, between the opening and closing class declaration brackets:

    private int someNumber;

    Any classes you create in your application that inherit from the abstract class will also contain this variable.

    3

    Add a method to your abstract class declaration. Your class may contain abstract and non-abstract methods. Insert a non-abstract method as follows:

    public void setNumber(int num)

    someNumber=num;

    Any extending classes will not need to implement this method, as they will automatically inherit it. This means any instances of subclasses of the abstract class will provide the "setNumber" method to other code.

    4

    Add an abstract method to your class. Any abstract methods declared within your class will have to be implemented by subclasses. This is where an abstract class functions in a way similar to an interface, as the abstract methods are a way of forcing extending classes to implement set methods. Add an abstract method to your declaration as follows:

    abstract public String getText();

    This means extending classes must provide this method complete with an implementation for it. Such practices are useful where you want subclasses to be able to tailor the method detail to suit themselves.

    5

    Extend your abstract class. Within your application, create another new class to extend your abstract one. The following is an example of a class extending the "AbstractDemo" class:

    public class AbstractDemoExtender extends AbstractDemo

    private String someText;

    public AbstractDemoExtender()

    someText="Hello";

    public String getText()

    return someText;

    Test your application by creating an instance of the class extending the abstract class, calling the methods on it to test its behavior.

Blog Archive