What are the four pillars of OOP?

The four pillars of Object-Oriented Programming are:

  • Encapsulation: The bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components.
  • Abstraction: The concept of hiding the complex reality while exposing only the necessary parts.
  • Inheritance: A mechanism in which one class acquires the property of another class.
  • Polymorphism: The ability of a variable, function, or object to take on multiple forms.

Explain polymorphism with examples from C++ or Java.

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

C++ Example (Runtime Polymorphism):


class Animal {
public:
    virtual void animalSound() {
        cout << "The animal makes a sound" << endl;
    }
};

class Pig : public Animal {
public:
    void animalSound() {
        cout << "The pig says: wee wee" << endl;
    }
};
                

Java Example (Runtime Polymorphism):


class Animal {
    public void animalSound() {
        System.out.println("The animal makes a sound");
    }
}

class Pig extends Animal {
    public void animalSound() {
        System.out.println("The pig says: wee wee");
    }
}
                

How does inheritance improve code reusability?

Inheritance allows a new class (the subclass or derived class) to inherit the properties and methods of an existing class (the superclass or base class). This promotes code reuse because you don't have to rewrite the code from the superclass in the subclass. You can also add new properties and methods to the subclass, or override the methods of the superclass to provide a more specific implementation.

What are abstract classes vs interfaces?

Abstract Class:

  • Can have both abstract and non-abstract methods.
  • Can have instance variables.
  • A class can inherit from only one abstract class.
  • Use an abstract class when you want to provide a common base class for several related classes.

Interface:

  • Can only have abstract methods (in Java 8, they can have default and static methods).
  • Cannot have instance variables.
  • A class can implement multiple interfaces.
  • Use an interface when you want to define a contract that can be implemented by multiple, unrelated classes.

How does exception handling work in C++ and Java?

Both C++ and Java use a similar mechanism for exception handling, based on the `try`, `catch`, and `throw` keywords.

  • try: A `try` block identifies a block of code for which particular exceptions will be activated. It's followed by one or more `catch` blocks.
  • catch: A `catch` block is where you handle the exception.
  • throw: A program throws an exception when a problem shows up. This is done using a `throw` keyword.
  • finally (Java only): The `finally` block is always executed, whether an exception is handled or not.

Compare object-oriented programming with functional programming.

Object-Oriented Programming (OOP):

  • Focuses on objects that encapsulate data and behavior.
  • State is mutable and managed within objects.
  • Primary goal is to model the real world.

Functional Programming (FP):

  • Focuses on pure functions and immutable data.
  • State is immutable.
  • Primary goal is to write predictable and bug-free code.

How would you teach encapsulation to first-year students?

I would use the analogy of a capsule. A capsule contains different medicines, but from the outside, you only see the capsule. You don't know the details of the medicines inside.

Similarly, in OOP, encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (a class). It also involves hiding the internal state of an object from the outside. This is achieved by making the attributes private and providing public methods to access and modify them.