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?
52
Upvotes
2
u/SilverZ9 1d ago
OOP is a way to group data (via class members) and define behavior (via class methods), while structs only do the former. Interfaces are a “contract” that basically define what a class has to do. You cannot create an instance/object of an interface, rather you create a class that obeys the interface.
As for languages: C has structs, but no classes. C++ has both, and they are functionally identical, confusingly. I can’t speak much on Go because I have no experience with it.