Tagged “oop”

  1. Demystifying Object-Oriented Programming: Pt. 2

    ~ cat post <<

    Demystifying Object-Oriented Programming: Pt. 2

    Welcome back! At the end of our last post, we hit a bit of a roadblock. We had successfully organized our F1, PickupTruck, and SUV classes to inherit from a base Car class. But then, we were faced with a new challenge:

    A bicycle, a speedboat, and an electric car. They are all vehicles, but trying to force them into a single Vehicle inheritance hierarchy would be a nightmare. A speedboat doesn't have wheels, and a bicycle doesn't have an engine in the traditional sense. A simple extends Vehicle starts to feel clunky and wrong. How do we model things that share behaviors but are fundamentally different things?

    The answer lies in moving beyond the idea that everything must share a common ancestor and instead thinking about what they can DO.

    Interfaces: A Contract of Behavior

    Let’s ask a different question. Instead of asking what these objects are, let's ask what they have in common from a user's perspective. A person can:

    • Steer them
    • Make them go forward
    • Slow them down

    In Object-Oriented Programming, when we want to guarantee that different classes share a common set of behaviors, we use an INTERFACE. Think of an interface not as a blueprint for an object, but as a contract. It’s a list of methods that a class promises to implement. It defines WHAT a class can do, but not HOW it does it.

    Let's create a contract for anything that can be driven. We'll call it Drivable.

    // Interface
    public interface Drivable {
      void turnLeft(double degrees);
      void turnRight(double degrees);
      void accelerate();
      void brake();
    }
    
  2. Demystifying Object-Oriented Programming: Pt. 1

    ~ cat post <<

    Demystifying Object-Oriented Programming: Pt. 1

    The Misconception of Object-Oriented Programming

    When we think about object-oriented programming, we often relate it to real-world objects. The problem is this: associating object-oriented programming with tangible real-world things doesn’t work in 80% of cases, and this brings more difficulties than clarity for beginners.

    Back in 1996, when I started programming in Delphi 4, its biggest competitor was Visual Basic, but Delphi was OBJECT-ORIENTED. If I had stopped at that programming course, I still wouldn't have known what this famous object-oriented programming was, and neither would the course instructor.

    I worked at various companies and on commercial software projects in Delphi that were far from truly using object-oriented programming. Most Delphi programmers I met thought object-oriented programming was about dragging a button onto a screen, double-clicking it, and creating a function for that button, for example.

    So, let’s talk about what object-oriented programming REALLY is.

    First, look at the image below and tell me what you see.

    An F1, a Pickup Truck, and an SUV

    Obviously, you see three cars: a pickup truck, an F1 car, and an SUV. The important question is: if these cars are so different, how do you know they are all cars? What makes a car a car and not a table or a piano?

    There’s a concept of what a car is that’s already ingrained in your mind. A car IS LIKE THIS and SERVES THIS PURPOSE. In object-oriented programming, this concept that defines what a car is called a CLASS: it’s the definition of WHAT a certain object is. The car you touch, the one you drive from point A to point B, is what we call an OBJECT. It’s the materialized concept. This materialized concept is technically called an INSTANCE. That’s why you’ll sometimes read that an object is an instance of a class. Always translate this as: the object you touch is a concept that has been materialized.

~ <<

See all tags .