Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects simply to a non-technical, business audience. Over…
In programming languages, an abstract class is a generic class (or type of object) used as a basis for creating specific objects that conform to its protocol, or the set of operations it supports.
An abstract class, in the context of Java, is a superclass that cannot be instantiated and is used to state or define general characteristics. An object cannot be formed from a Java abstract class; trying to instantiate an abstract class only produces a compiler error. The abstract class is declared using the keyword abstract.
Subclasses extended from an abstract class have all the abstract class's attributes, in addition to attributes specific to each subclass. The abstract class states the class characteristics and methods for implementation, thus defining a whole interface.
Abstract classes are useful when creating hierarchies of classes that model reality because they make it possible to specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class (a derived class) is needed.
In object-oriented programming (OOP) languages, classes represent objects in the domain of the problem the software is intended to solve. Classes include collections of attributes (properties) and behaviors (methods), which can be based on previously-defined classes. Programmers use inheritance to derive the specific implementation of abstract classes. Classes that are derived from abstract classes are called derived classes. When this principle is applied many times in succession, it results in a hierarchy of classes. In this context, abstract classes are at the root of this hierarchy, and is used to enforce methods that need to be overridden in the derived classes, thus avoiding potential runtime errors.
Abstract classes serve as templates for their subclasses. For example, the abstract class Tree and subclass, Banyan_Tree, has all the characteristics of a tree as well as characteristics that are specific to the banyan tree.
Understanding the differences between an abstract class and an interface is essential. An interface only has method declarations or abstract methods and constant data members, while an abstract class may have abstract methods, member variables and concrete methods. Because Java only supports single inheritance, a class can implement several interfaces but can extend only one abstract class.
An abstract class has at least one abstract method. An abstract method will not have any code in the base class; the code will be added in its derived classes. The abstract method in the derived class should be implemented with the same access modifier, number and type of argument, and with the same return type as that of the base class. Objects of abstract class type cannot be created, because the code to instantiate an object of the abstract class type will result in a compilation error.
Techopedia’s editorial policy is centered on delivering thoroughly researched, accurate, and unbiased content. We uphold strict sourcing standards, and each page undergoes diligent review by our team of top technology experts and seasoned editors. This process ensures the integrity, relevance, and value of our content for our readers.
Margaret is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages.
What is GitHub? GitHub is a vast platform that changes how software developers work together on projects. It's more than...
Maria WebbTechnology journalist
What is a Super App? A super app is a comprehensive mobile or web-based platform that offers a wide range...
Nicole WillingTechnology Journalist
What is Windows AI Studio? Windows AI Studio is a software development environment that allows developers to build generative AI...
Margaret RouseTechnology Expert
Trending NewsLatest GuidesReviewsTerm of the Day