Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
So, 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.
Am 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,202 Points
My 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.