r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

2

u/CodeMonkeeh Apr 06 '22

I Googled it a bit just now, and here's what it looks like it does (someone please correct me if I'm wrong). The default implementation in interfaces forces all subclasses to use that implementation. In abstract classes, the subclasses can override the default implementation simply by creating its own implementation. But here, if you try to create your own implementation in the subclass it will not compile.

If I'm understanding you right, then no.
Default implementations in interfaces can absolutely be overridden by implementing types.

https://dotnetfiddle.net/KBbQfy

2

u/SavvyPlatinum Apr 06 '22

Huh, interesting thanks for checking. I guess the main difference then is that because interfaces can't have instantiated variables, the methods will essentially have to be static methods. But yeah the method works the same as if it was in a class.

I hate it lol.

u/Brafaan please disregard my answer about the C# interface. Methods can indeed be overridden even with an interface.

1

u/CodeMonkeeh Apr 07 '22

Static interface members is a separate thing.

You can have an instance member with a default implementation.

IIRC they had two reasons for this feature:

  1. It allows you to add members to an interface without breaking every implementing type. This is especially nice for library authors.

  2. It basically acts as the traits concept.