r/javahelp • u/The_King1337 Intermediate Brewer • Jun 19 '13
When would I use interfaces?
I've recently learned about interfaces, but when would I actually use them? Thanks!
17
Upvotes
r/javahelp • u/The_King1337 Intermediate Brewer • Jun 19 '13
I've recently learned about interfaces, but when would I actually use them? Thanks!
2
u/dudleydidwrong Jun 19 '13
One practical issue is that you can only inherit from one class. Some languages lik C++ allow multiple inheritance, but experience suggested that unrestrained multiple inheritance can take you to hell in a handbasket very quickly. A set of guidelines were developed fo C++ programmers to help keep them out of the deep end of the multiple inheritance cess pool. One of these guidelines said to only inherit data fields from one class; all the other classes should be pure virtual classess. Java formalized this concept to an interface. So in Java you can extend one full calss, but you can use as many interfaces as you want.
I think the best way to learn about interfaces and motivations for using them is to learn about GUI and event-driven programming. Swing uses just s about every feature of classes and interfaces. You may not like Swing for other reasons, but it is useful as a learning tool.