interview-questions

OOP Design

1. What is OOP and what are the 4 pillars of OOP?

  1. Encapsulation:
    • Description: Encapsulation is the bundling of data (attributes) and methods that operate on the data into a single unit, or class. It restricts direct access to some of an object’s components, which is a means of preventing accidental interference and misuse of the methods and data.
    • Benefits: Increases security and hides the implementation details from the user.
  2. Abstraction:
    • Description: Abstraction involves hiding complex implementation details and showing only the necessary features of the object. It helps in reducing programming complexity and effort.
    • Benefits: Simplifies the view of an object, and separates the interface from the implementation.
  3. Inheritance:
    • Description: Inheritance is a mechanism where a new class is derived from an existing class. The new class, known as a subclass, inherits attributes and methods of the existing class, known as a superclass.
    • Benefits: Promotes code reuse, and establishes a relationship between different classes.
  4. Polymorphism:
    • Description: Polymorphism means ‘many forms’. It allows objects of different classes to be treated as objects of a common superclass. It can be achieved by overriding (same method names in different classes) or overloading (same method name, different parameters).
    • Benefits: Enhances flexibility and integration by allowing the same interface for different underlying forms (data types).

2. What is an object and what is a class?

Class

Object

Relationship Between Class and Object

3. What is multiple inheritance and what is specific about it?

Multiple inheritance is a feature of some object-oriented programming languages in which a class can inherit behaviors and characteristics from more than one parent class. This contrasts with single inheritance, where a class may only inherit from one parent class.

4. What does “favor object composition over class inheritance” mean?

It means that code reuse should be achieved by assembling smaller units of functionality into new objects instead of inheriting from classes and creating object taxonomies. In other words, use can-do, has-a, or uses-a relationships instead of is-a relationships. Using this approach we:

5. What is the diamond problem?

A notable issue where a class inherits from two classes that both inherit from a common superclass, potentially causing ambiguity in which version of the superclass’s attributes and methods the subclass should inherit.

6. What is the Banana Monkey Jungle problem?

The Banana Monkey Jungle problem is a metaphor used in software engineering to describe a situation where a developer wants a ‘banana’ (a specific piece of functionality) but ends up getting the ‘monkey’ (the module or class that contains the functionality) and the entire ‘jungle’ (the whole environment or framework on which the module depends).

Key Points

Implications

Solutions