u/Xelestisin r/compsci • Recommends
On “Why do we use interfaces instead of directly inheriting the dependent class?”
Yup the common take on interfaces is that they are contracts, and if you inherit it then you need to implement all of its methods.
The original reason interfaces were introduced in C with objects was to reduce coupling, yes. Think of a dependency tree with high level modules at the top, and low level ones down at the bottom. Without interfaces, if I change code in a tiny module at the bottom then everything has to recompile all the way up to the top instead of just that single module. That makes the entire tree more fragile, and our compile time is a lot longer
So in short, its to reduce coupling. I really first started appreciating interfaces when I learned about SOLID coding, and a good…