Object-Oriented Programming Problems

Problems you are presented with 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.

Interfaces and Abstract Classes

PROBLEM What is the difference between an interface and an abstract class in object-oriented programming?

The specific answer to this depends on the language, but some general definitions are:

  • 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 its methods.

Conceptually, then, an interface defines an application programming interface (API) that is independent of any class hierarchy. Interfaces are particularly important in languages that support only single inheritance, in which classes can inherit only from one base class. A class that includes all the methods described in an interface is said to implement the interface.

Unlike an interface, an abstract class is a proper class: It can have data members and method definitions and can be a subclass of other classes. Unlike a concrete (nonabstract) class, some of its behaviors are deliberately left to be defined by its own subclasses. Abstract classes cannot be instantiated because of this — only instances of concrete subclasses can be created.

An interface is equivalent to an abstract class with no data members and no method definitions. ...

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

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