Abstract type

In programming languages, an abstract type (also known as existential types)[1] is a type in a nominative type system that cannot be instantiated directly; by contrast, a concrete type can be instantiated directly. Instantiation of an abstract type can occur only indirectly, via a concrete subtype.

An abstract type may provide no implementation, or an incomplete implementation. In some languages, abstract types with no implementation (rather than an incomplete implementation) are known as protocols, interfaces, signatures, or class types. In class-based object-oriented programming, abstract types are implemented as abstract classes (also known as abstract base classes), and concrete types as concrete classes. In generic programming, the analogous notion is a concept, which similarly specifies syntax and semantics, but does not require a subtype relationship: two unrelated types may satisfy the same concept.

Often, abstract types will have one or more implementations provided separately, for example, in the form of concrete subtypes that can be instantiated. In object-oriented programming, an abstract class may include abstract methods or abstract properties[2] that are shared by its subclasses. Other names for language features that are (or may be) used to implement abstract types include traits, mixins, flavors, roles, or type classes.

Abstract types may also include any number of non-abstract methods and properties, such as when implementing the Template Method Pattern which uses a mixture of invariant methods with fixed implementations and hook methods which can be overridden in concrete subclasses to provide custonised logic.

Creation

Abstract classes can be created, signified, or simulated in several ways:

  • By use of the explicit keyword abstract in the class definition, as in Java, D or C#.
  • By including, in the class definition, one or more abstract methods (called pure virtual functions in C++), which the class is declared to accept as part of its protocol, but for which no implementation is provided.
  • By inheriting from an abstract type, and not overriding all missing features necessary to complete the class definition. In other words, a child type that does not implement all abstract methods from its parent becomes abstract itself.[2][3]
  • In many dynamically typed languages such as Smalltalk, any class that sends a particular method to this, but does not implement that method, can be considered abstract. (However, in many such languages, like Objective-C, the error is not detected until the class is used, and the message returns results in an exception error message such as "Does not recognize selector: xxx" as - [NSObject doesNotRecognizeSelector:(SEL)selector] is invoked upon detection of an unimplemented method).

Examples

Java

By default, all methods in all classes are concrete, unless the abstract keyword is used. An abstract class may include abstract methods, which have no implementation. By default, all methods in all interfaces are abstract, unless the default keyword is used. The default keyword can be used to specify a concrete method in an interface.

//By default, all methods in all classes are concrete, unless the abstract keyword is used.
public abstract class Demo {
    // An abstract class may include abstract methods, which have no implementation.
    public abstract int sum(int x, int y);

    // An abstract class may also include concrete methods.
    public int product(int x, int y) { 
        return x * y; 
    }
}

//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface {
    int getLength(); //The abstract keyword can be used here, though is completely useless
    
    //The default keyword can be used in this context to specify a concrete method in an interface
    default int product(int x, int y) {
        return x * y;
    }
}

Usage

Abstract types are an important feature in statically typed OOP languages. Many dynamically typed languages have no equivalent feature (although the use of duck typing makes abstract types unnecessary); however traits are found in some modern dynamically-typed languages.

Some authors argue that classes should be leaf classes (have no subtypes), or else be abstract.[4][5]

Abstract types are useful in that they can be used to define and enforce a protocol; a set of operations that all objects implementing the protocol must support.

Abstract types are also an essential part of the template method pattern.

When to Use Abstract Classes

Abstract Classes and Methods are important concepts in object-oriented programming languages. An abstract class defines shared properties and behaviors, while leaving certain methods for subclasses to implement.

Consider an Employee example, often used in payroll or HR management systems. The base class is Employee, which defines shared attributes such as name, ID, and salary, along with general methods like calculatePay(). Specific types of employees—such as Manager, Engineer, and Intern—are derived from this base class, each adding their own unique attributes and behaviors.

For example, a Manager may have a list of team members and a performance bonus, an Engineer may have a specialty and project assignments, and an Intern may have a duration and mentor assigned. The way each employee’s pay is calculated or tasks are handled may differ. This hierarchy demonstrates both the common properties shared by all employees and the distinctive features of each role.[6]

Features of Abstract Class

In Java, an abstract class provides an efficient way to achieve data abstraction by hiding the implementation details. It also acts as a template that defines the methods to be implemented by subclasses. Data abstraction encourages loose coupling in Java by greatly reducing interdependencies within the program. Abstract classes enhance reusability, as abstract methods can be invoked wherever required, removing the need to write duplicate code. By concealing complex implementation details and exposing only the essential features, abstraction allows developers to simplify code for end-users. With the help of dynamic method resolution, a single abstract method can be applied to handle different scenarios, making problem-solving more flexible.[7]

See also

References

  1. ^ Mitchell, John C.; Plotkin, Gordon D.; Abstract Types Have Existential Type, ACM Transactions on Programming Languages and Systems, Vol. 10, No. 3, July 1988, pp. 470–502
  2. ^ a b "Abstract Methods and Classes (The Java Tutorials > Learning the Java Language > Interfaces and Inheritance)". Oracle.com. Retrieved 2019-08-14.
  3. ^ "Pure Virtual Functions and Abstract Classes in C++". GeeksforGeeks.org. 15 July 2014.
  4. ^ Riel, Arthur (1996). Object-Oriented Design Heuristics. Addison-Wesley Professional. p. 89. ISBN 0-201-63385-X.
  5. ^ Meyers, Scott (1996). More Effective C++. Addison-Wesley Professional. p. 258. ISBN 0-201-63371-X. Make non-leaf classes abstract
  6. ^ "What is Abstraction in Java? Advantages, Types & Examples". www.ccbp.in. Retrieved 2025-09-19.
  7. ^ "What is an Abstract Class in Java and How to Implement It?". Simplilearn.com. Retrieved 2025-09-19.

Further reading

  • Head First Java. O'Reilly Media. 2003. pp. 688. ISBN 0-596-00920-8.
  • Core Java: An Integrated Approach by R. Nageswara Rao