Properly structuring your code and data is essential. Object-oriented programming (OOP), an extension of imperative programming is one of the most commonly used methods.
This reference covers the commonly used object-oriented concepts. Though not covered here, you should also know when to use these features. Knowing how to abstract correctly improves code quality.
The terms used to describe objects, types, and variables, vary between languages. Mostly there is no conflict and the core concepts translate easily. Knowing the idea behind the term helps in situations where the languages diverge and is beneficial to understanding programming literature and communicating in an interview.
Most coding interviews will not likely be involved enough to use more than the basic class concepts. Though take-home projects may require all the items listed here.
Every language offers a way to group data and functions. This should be second nature to you in an interview, otherwise you risk being too slow.
- Define a class: Include a constructor and some member variables.
- Instantiate a class: Use the class you defined, passing arguments to the constructor.
- Release or cleanup an instance: Whether this is automatic, manual, or by convention, cleaning up is essential for resource constraint classes (such as files).
- Define a class or static variable: These variables live for the entire duration of the program execution.
- Data structure: Some languages have different constructs or conventions for pure data structures.
Visibility is an important aspect of OOP. Not all languages support visibility constraints, but may use conventions instead.
- Public/Protected/Private fields: Declare a variable and function with each of the common visibilities. Understand the differences between private and protected, and potentially class and package visibility for your language.
- Getter: Write a public getter for a private field, or calculation.
- Setter: Write a setter for a private field.
Overriding functions in a class hierarchy is one pillar of polymorphic programming.
- Define a virtual function: Write a class that contains a virtual function.
- Override a virtual function: Write a derived classes the overrides the virtual function.
- Call super class function: Inside the override, call the base class version of the function.
- Final function: Define a function that cannot be overridden by a derived class.
Interfaces are a common design pattern. Some languages have a syntax for them, others use the abstract and virtual mechanism, and others work by convention.
- Define an interface: Include a few functions and at least one variable.
- Implement an interface: Create an implementation of the interface you defined.
- Use an interface: Write code with your implementation that calls a function taking only the interface.
Abstract classes define an incomplete structure that a derived class needs to complete.
- Define an abstract class: An abstract class has at least one abstract method.
- Declare an abstract function: A function without a body that a derived class must implement.
- Implement an abstract function: Derive from an abstract class and complete the missing functions, creating a non-abstract class.
Though we prefer to work abstractly with classes, there are situations where knowing the type, or casting to the correct type is desired.
- Identify: Given an object what is the type of it? This is most useful for debugging and logging.
- Compare: Compare if an object is equal to, or convertible to a specific type. This is usually limited to related types (within the same class hierarchy).
- Downcast: Convert a generic base type reference to a more specific derived type.