What Is an Abstract Class?


what is an abstract class

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

The abstract class is an important aspect of object oriented programming. Its prominence in OOP languages like Java and C++ makes it one of the prime questions asked during programming interviews. Therefore, here are the accurate answers for the question ?what is an abstract class.’

What is an Abstract Class in Programming?

An abstract class in OOP is a class that has one or more abstract methods, which must be implemented by subclasses. Abstract methods are methods which are only declared and not implemented. They usually end in a semicolon rather than the curly braces used in main methods.

How an Abstract Class Works

Abstract classes work just like interfaces but with notable differences. You can instantiate both abstract classes and interfaces, and they can also have different methods declared without an implementation. When defining an abstract class, your statement should begin with the keyword ?abstract? followed by the definition of your class. An example of an abstract class is as follows:

abstract void myAbstractExample{}

It is imperative to note that abstract classes cannot be instantiated. Instead, they are subclassed. When you sub-class the abstract class, the subclass will provide you with implementations for the abstract methods contained in the class. However, if it doesn’t, you must declare the subclass also as abstract. An abstract class can also contain a concrete method instead of an abstract method.
An abstract class allows you to declare fields which aren?t static and final. This aspect is particularly important when defining methods that can access and modify the instance of an object. Abstract classes also allow you to define public, protected, and concrete methods efficiently. Interfaces restrict you to declaring all fields as public whenever they are used as default methods.

When Should You Use Abstract Classes?

Abstract classes are a great option if you want to share a common code among several related classes. By using the ?extends? function, it allows inheritance between your abstract class and other similar classes in your code. You should also use an abstract class if you suspect that the classes which inherit it have same methods or fields and require access modifiers like ?protected? and ?private.’

In Java, AbstractMap is a good example of an abstract class. It has subclasses such as HashMap, TreeMap, and ConcurrentHushMap. All these subclasses are known to share numerous methods defined by the abstract class AbstractMap. Some of those methods include get, put, isEmpty, containsKey, and containsValue.

Note: If you want to get an overview of abstract classes, we also recommend reading our article explaining the differences between abstract class vs. interface.?

The Advantages of an Abstract Class

The main advantage of abstract classes is that they allow the generalization of their methods when a class inherits from them. This aspect allows code reusability. In a design perspective, an abstract class helps to simplify your code. It allows you to enforce base functions for a class. From this base class, you can develop other instances or methods to describe an object in different classes through inheritance.

These are some of the answers for the programming interview question ?what is an abstract class.’ Share your comments and thoughts about the above description in the comments section.

Recent Posts