Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialUnsubscribed User
9,496 PointsSo, in other words...
Implementing an interface forces certain methods to be added in a class? I am having trouble understanding the purpose of Interfaces, since no functionality is actually written within one. Why create an interface, rather than just add the methods into the class to begin with? I can see how they work, but not necessarily why
For example, if I have an interface with on/off methods, and have a TV, and a Radio class, that both implement that interface, I am still writing on/off methods for both classes, AND an extra step of writing an interface that is forcing me to write those on/off methods.
2 Answers
Sky King
Courses Plus Student 95 PointsAm in the same shoes as you, really finding it difficult to understand why writing interfaces is important. Hope someone gets to explain this concept to me better
Glenré Charl Labuschagné
23,204 PointsMy take is: you use interfaces to create a "template" for minimum required methods in class object(s). Thus enforcing certain rules/expectancies, that will error/alert you, if not met. Interfaces are normally used and implemented as a framework for others to use.