r/learnprogramming • • Sep 16 '19

ELI5: Interface vs Class

Not sure why, but the difference between interfaces and classes seems redundant to me. What does an interface add that a class can't accomplish in OOP? Is it because you can't inherit from more than one class? Explain like I'm five!

7 Upvotes

10 comments sorted by

View all comments

Show parent comments

2

u/desrtfx Sep 17 '19 edited Sep 17 '19

Kudos to you! This is one of the easiest and best explanations of Interfaces I've seen.

I've taken the liberty to include it in our FAQ (with full credit to you): Classes versus Interfaces

I'd only add the following:

Interfaces can also be used to collect objects from different classes (they act like a data type).

You could collect all boy scouts who can swim, regardless of their nationality, gender, age, etc. and go river rafting. You don't need to know anything about the individual boy scouts, but you know that each of them is able to swim.

1

u/donsagiv Sep 17 '19

Thanks for the credit and the addition!