9.3. Object-Oriented Programming Problems

Problems you are presented relating to object-oriented programming are likely to focus on the concepts of object orientation, particularly on issues relevant to the languages the company is using in its coding.

9.3.1. Interfaces and Abstract Classes


Explain the difference between an interface and an abstract class in object-oriented programming.

The specific answer to this depends, of course, on which language you're developing with, but start the answer by giving some general definitions:

  • An interface declares a set of related methods, outside of any class.

  • An abstract class is an incomplete class definition that declares but does not define all of its methods.

Conceptually, then, an interface defines an application programming interface (API) that is independent of any class hierarchy. In fact, interfaces can be used in non-OO programming models, such as component-based models like COM and CORBA. However, you're focusing on the use of interfaces in an object-oriented context, where they are useful in their own right. Interfaces are the ultimate encapsulators, because they hide all the details of the classes that implement their methods from the user of the interface. They're particularly important — almost necessary, in fact — in languages that only support single inheritance (classes can only inherit from one base class). A class that exposes its members via an interface is said to implement the interface.

Unlike an interface, ...

Get Programming Interviews Exposed: Secrets to Landing Your Next Job, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.