+ 5

What's purpose of interface if code works without?

https://code.sololearn.com/ciuzDlT9gqGH/?ref=app It makes no sense.

12th Jul 2018, 2:30 AM
Andre Daniel
Andre Daniel - avatar
3 Réponses
+ 6
The whole point of OOP is that you want to write reusable, modularized code. If you are writing libraries and code which can be reused in other projects, but you want to provide the user the ability to implement their own algorithms at achieving something, interfaces come in handy. Yes, you can also opt to override a method in the parent class, but: If you wrote an interface, an somebody implements your interface, it is mandatory for that somebody to implement all methods in the class which implements the interface, so that gives you a certain control over how your interface is used.
12th Jul 2018, 3:34 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Also, interfaces know that something must be done but do not know HOW to do it. Interface users know how to DO the interface stuff, customized for themselves: https://code.sololearn.com/cwm7QJbAMg8v/?ref=app They also help collections and looping. In that code, I don't need to mess around with separate variables for each shape. I just push whatever shape is next as a Drawable. At display time, I can easily loop and call draw() on whatever pops out next.
12th Jul 2018, 5:08 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Funny I wanna know the same thing.
12th Jul 2018, 2:51 AM
Andre Daniel
Andre Daniel - avatar