What's purpose of interface if code works without?

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

7/12/2018 2:30:10 AM

Andre Daniel

3 Answers

New Answer


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.


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.


Funny I wanna know the same thing.