Abstract classes in Java

Facebooktwitterredditpinterestlinkedinmailby feather

Abstract classes in Java

Java uses the ‘abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction.

In programming language, abstract classes are generic classes (or types of objects) used as a basis to create particular objects that conform to its protocol, or the set of operations it supports.

Some important points regarding abstract classes

  • An Abstract class is a class that contains abstract as well as concrete methods.
  • An Abstract class can’t be instantiated in other words. We cannot create the object of an abstract class.
  • An abstract class is a class that has at least one abstract method, and maybe there can be some regular methods.
  • An abstract class can have static methods and constructors.
  • An abstract class can have final methods which will prevent the subclass from changing the body of the method.
  • We can achieve 0 to 100 percent abstraction using Java abstract classes.

In an object-oriented programming language, classes contain the objects, attributes (properties) and behaviors (methods) which can be based on previously defined classes. Java uses inheritance to derive the specific implementation of abstract classes. When this principle applied for many successions, it creates a hierarchy of classes.  In this situation, abstract classes are at the top and are used to enforce methods that need to be overridden in the subclasses. This process avoids potential runtime errors.

Declaration of abstract class

Rules for abstract class

Here are some points that should be remembered before you make an abstract class. We can call them as rules

  • To use an abstract class, we have to inherit it from the other class.
  • To use an abstract class, you need to create another class which extends this class.
  • A class derived from a parent abstract class must contain all the methods of the parent class.
  • The object of the abstract class cannot be created.
  • If the child class does not contain all the method of a parent class, then it should be declared as an abstract class.

Why we need an abstract class?

Let’s assume a Student class that contains method info() and its subclasses, name, id etc.. Since every name contains a different id like ‘a’ contain id 111 and ‘b’ contains id 222, so there is no point to implement this method to parent class because every child class overrides the method to provide its implementation detail.

So when we know that all the student classes will override this, then there is no point to implement this method in the parent class. Thus making this method abstract is a good option. By making this method abstract, we force all the child classes to implement this.

Abstract methods

An abstract method is a method that has only the method definition. An abstract method does not have implementation and body. It defines only the signature of the method. Syntax  abstract void method(double X, double Y);

If a class contains at least one abstract method, then it should be declared as abstract.

public abstract class Demo{   // declare fields   // declare nonabstract methods   abstract void method();}

If an abstract class is subclassed, then subclass will provide the implementation for all abstract method of the parent class. However, if it will not provide the implementation then the subclass must be declared as abstract.

Some scenarios of Abstract class

Example 1

Output

In the given example, Food is the abstract class and its implementation is provided by the Pizza and Burger classes.

Mostly, implementation detail is hidden for us (which is hidden to the end user), and an implementation class object is provided by the factory method.

Example 2

This example explains that a Java program may contain an abstract class, an abstract method, method body (non-abstract method), data member, constructor, and even main() method.

Have a look at the below example.

Output

Facebooktwitterredditpinterestlinkedinmailby feather