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 trialyadin michaeli
Courses Plus Student 5,423 PointsDidn't get interface
I didn't get nothing about interface! please can somebody help me to understand this? :(
1 Answer
Ryan Sherry
12,449 PointsHi Yadin,
I am happy try and help shed some light on interfaces.
From what I have gathered so far in our classes and in my personal learning, an interface is like a contract and it defines what variables and methods must be created if you decide you want a class to implement it. When you create an interface, it is like creating a class but the methods don't have bodies and the variables are not instantiated. Here is an example:
public interface fillWaterBottle { public static final int MAX_WATER; public int waterLevel; public void fillWaterBottle(); }
In the interface above, if we want one of our classes to implement it, we must follow the rules and adhere to the contract by creating a public void fillWaterBottle() method in our class. The cool thing is that we can decide what we want the method to actually do:
public class waterBottle implements fillWaterBottle { public static final int MAX_WATER = 10; public int waterLevel = 0; public void fillWaterBottle() { waterLevel = MAX_Water; } }
I hope this helps!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsThanks Ryan for the clear explanation!