Internet
Fact-checked

At WiseGEEK, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What Is the Difference between an Abstract Class and an Interface?

Jessica Susan Reuter
Jessica Susan Reuter

Both an abstract class and an interface are specific types of computer objects that allow a programmer to loosely define one type of object as if it were another type, while retaining all of the object's original properties. While multiple different computer languages use one or both of these concepts, Java® is the most well-known. Abstract classes and interfaces have a variety of similarities, but also incorporate significant differences in structure, syntax, and usage.

Flexible object typing works almost exactly the same for an abstract class and an interface. If an abstract class Example exists, and a class Example2 is derived from that class, an instance of class Example2 can be used as an instance of Example or Example2, depending on the relevant context. By the same mechanism, if a class Example3 inherits from an interface called Example4, an instance of Example3 can be used as an instance of Example3 or Example4, where necessary. In both of these cases, classes that do distinct things but have some similarities can be used as if they were all the same type.

Woman doing a handstand with a computer
Woman doing a handstand with a computer

An abstract class and an interface can both be used in inheritance, where one or more classes are derived from a base class. Despite this similarity, the mechanism in which they are used is somewhat different. Abstract classes can function as both base classes or derived classes, while interfaces can function only as base classes. Neither an abstract class nor an interface can be instantiated on their own; only the classes derived from them can be instantiated, meaning that an instance of an object is created. Though they can function similarly to base classes in inheritance, interfaces are not classes, since they may contain only function prototypes and constants.

Interfaces are much more flexible than abstract classes, because a class can inherit from only one base class, but any number of interfaces. An abstract class and an interface can be combined by inheritance; abstract classes, like concrete classes, can inherit from multiple interfaces. Abstract class inheritance in Java® enforces that language's rule prohibiting multiple inheritance, since a concrete class cannot inherit directly from one or more abstract classes. Interface inheritance, on the other hand, allows Java® to emulate multiple inheritance by incorporating functions and data from multiple interfaces into one class.

Both an abstract class and an interface are somewhat limited on their own, but when combined with other objects, or especially when used in inheritance, they allow for extremely flexible type handling. A special inheritance pattern called polymorphism can make use of either of these language constructs. Using these constructs does not necessarily protect a programmer from poor inheritance structure; they must be used just as carefully as concrete classes.

Discuss this Article

Post your comments
Login:
Forgot password?
Register:
    • Woman doing a handstand with a computer
      Woman doing a handstand with a computer