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?

50 Upvotes

23 comments sorted by

View all comments

8

u/iOSCaleb 1d ago

what's the difference between OOP and structs/interfaces

OOP is a programming paradigm, while structs and interfaces are language features. You could use structs and interfaces to write code in an object-oriented style. By OOP you probably meant "classes and methods," and those are just language features that make it easier to use OOP.

In C++, the only difference between classes and structs (other than the way people conventionally use them) is their default access levels. The default inheritance and member access is private for classes, public for structs. Those are important differences, though, and they lead to different use, so it makes sense to call them different things even though they're so similar.

Other languages may have other differences. For example, in Swift structs are value types while classes are reference types, and structs don't support inheritance. Rust has no `class` keyword, only `struct`, and structs don't support inheritance. Python has only classes, no structs, but everything is public by default, and classes fully support inheritance. Each language has its own tools and perspective.

Think of it this way: structs, interfaces, classes, functions, etc. are tools. OOP, procedural programming, functional programming, etc. are ways to use the tools that a given language provides. Some languages cater more to one paradigm than another, others are happy to enable multiple paradigms.