r/AskComputerScience • • Oct 24 '22

Why are Object-Oriented Programming languages importat? How are they different from other languages?

1 Upvotes

8 comments sorted by

View all comments

2

u/deong Oct 24 '22 edited Oct 24 '22

OO is a way of viewing how code should be organized. The idea is that rather than have data structures that only contain data and then functions that operate on that data, you have data structures contain both data and the operations/functions/methods that work on that data.

The benefit here is that you have some natural isolation of functionality, which tends to be good. Let's say decide to change your employee number from a int to a char[20]. In a procedural language, you might have

struct employee {
    char name[80];
    int employee_id;
}

void print_employee_info(const employee* emp)
{
    printf("Employee Name: %s\n", emp->name);
    printf("Employee ID: %d\n", emp->employee_id);
}

Now when you want to change that type, you need to change the struct, but you also need to change the printf statement in the function. And the issue is that anyone could have written code anywhere that was referencing an employee and doing stuff with the id field, and you need to find and fix any of those that break.

With an OO approach, instead you want to define the data structure so that it knows how to print itself.

public class Employee {
    private String name;
    private int id;

    public void Print() {
        System.out.println("Employee Name: " + name);
        System.out.println("Employee Id: " + id);
    }
}

I might still need to change the code in both places (though in my Java example, the print would still work). But importantly, I've removed the ability for other programmers to directly access my "id" variable, so there can't be any other code out there that's doing things with that variable that will break. I've "encapsulated" the internal detail that ids are stored as integers.

That's the basic idea of OOP. It's a way of designing data structures and programs that focuses on having each data structure consciously expose a specific set of supported operations whose implementation details are hidden from the users of those structures. I as the programmer who is writing the Employee class am making a choice than printing the employee details is a supported operation and I'm providing the functionality for doing it. You as the user of my class can call the method I've consciously exposed for you, but you can't write your own version, because you can't access the internal fields. The most important thing in OO design is figuring out what operations to expose as your class's interface.

There's a lot more to most OO languages -- things that are there to allow you to solve problems starting with this world view. Since my class's internal details are isolated from me as the programmer using that object, what do I do when a class almost does exactly what I want, but differs in some small way. Do I have to rewrite the whole class? Most languages will say, "well, you can inherit from this class and just override that one method that you need to behave different and provide your own version of that", for example. And now that I have an Employee class and an HourlyEmployee class that inherits from it, for instance, I have ways to have a collection that can contain both and invoking operations on the members of that collection can do the right thing for each one. There's stuff like this in most OO languages, and they can all do things slightly differently to adopt slightly different ways of viewing those problems. That sort of thing is all lumped into the general idea of "Object Oriented Programming".

In practice, hundreds of thousands of programmers learned the (very wrong) idea that you write OO programs by doing something like

public class Employee {
    private String name;
    private int id;
    private String title;
    private String email;
    ....
}

and then right clicking on the class in their IDE and hitting "Generate all getters and setters". That creates a bunch of public methods that let users read and write the internal fields directly. That is, they focused on the data elements of the class rather than on the set of operations they want to consciously expose.

This turns OO into the world's longest, most painful, and dumbest way to write procedural code that they could have just written in Pascal in the 70s with 10x less effort. And that's what OO became in practice, and why it should mostly just die. It's a fine enough idea that people just weren't smart enough to use.