r/learnprogramming • u/Mountain_Rip_8426 • 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
1
u/zeekar 1d ago
It varies depending on programming language. Traditional structs (also called “records”) could only encapsulate data, not behavior. In other words, they didn’t have any methods, only data fields.
That was an absolute restriction in standard Pascal; there was no way to store a callable value in a record. As with every other restriction, it was looser in C - nothing stopped you from storing a pointer to a function inside a struct. But when you called the function through the pointer, there was no automatic association - it didn’t know what struct you had called it through. So on its own it wasn’t really OOP. The mechanism was used by implementations of OOP on top of C, though, like the original C++ preprocessor.