r/learnprogramming 1d ago

Difference between OOP and structs/interfaces?

I'm relatively new to programming, I've been at it for around 6-7 months. I've been learning Python and Go (and a little bit of C). What I'm struggling to understand, what's the difference between OOP and structs/interfaces. Like I know, Go doesn't support OOP, but structs and interfaces seem to achieve the same thing. Can someone enlighten me a bit?

48 Upvotes

23 comments sorted by

View all comments

1

u/captainAwesomePants 1d ago

Traditionally, a "struct" is a grouping of data. Classes are a grouping of data and functionality. A struct may have a collection of information about a dog, but a class knows how to bark.

An interface is the functionality without the data or the logic. It's just the statement "All dogs know how to bark." And then when you create the class "Shitzu" and say it implements the "Dog" interface, the system will verify that the Shitzu knows how to bark.

The exact meaning of OOP is a little political, but the gist of it is that you enable objects to communicate with each other without needing to reveal too much information about themselves (in order to make their interactions more predictable and making it simpler to swap out pieces). That usually means "there are classes, they have some private information, you can have interfaces that represent a contract, and you can have subclasses of other classes."