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 trialArif Zuhairi
1,637 PointsHow to use the method if we don't define it?
In the video show that the definitions of function howToRide is in the class..so what's really mean of not having to define how these methods are handled?? Can anyone explain to me..if you can show some example of code..I'm really appreciated it
3 Answers
Jose Soto
23,407 PointsAn interface is a blueprint for a class. We tell it which methods every class that implements it must have.
For example, let's say we're going to make a blueprint (interface) for smartphones. What must every smartphone have?
- a way to access the internet
- a way to send a text message
The list can go on. However, each smartphone is different. You would use Safari on an iPhone to access the internet, but you would use Chrome on an Android. You would use Messages on an iPhone, but Hangouts on an Android. We can also think about Microsoft smartphones and smartphones running Firefox OS and Blackberry.
So how do we create an interface that will help us make classes for each of these smartphones? Easy! Just tell the interface that our class will need to have a way to access the internet. We don't need to know exactly how it will do that just yet. You can worry about that when you define the actual class.
Here is a rough example of what I am referring to:
<?php
// We just want to make a blueprint for all smartphones.
// Every class that implements this interface must use both of these methods.
interface smartphone
{
public function sendText($text_message);
public function openWebsite($url);
}
//When we define the class, THEN we actually have to worry about
//how the method is going to work.
class iPhone implements smartphone
{
public function sendText($text_message)
{
//Open Messages
//Send Text
}
public function openWebsite($url)
{
//Open Safari
//Go to URL
}
}
class Microsoft implements smartphone
{
public function sendText($text_message)
{
//Open Skype
//Send Text
}
public function openWebsite($url)
{
//Open Internet Explorer
//Go to URL
}
}
class Android implements smartphone
{
public function sendText($text_message)
{
//Open Hangouts
//Send Text
}
public function openWebsite($url)
{
//Open Chrome
//Go to URL
}
}
Every class the implements the smartphone interface MUST use both of the methods within the interface. If you do not, then you will get an error message. You can't have a smartphone without a way to get on the internet!!
Hope that helps a bit. Cheers!
Arif Zuhairi
1,637 PointsOh great..that clear it up ; ) Now I understand clearly..Thanks Jose
Jonah Shi
10,140 PointsThank you Jose, I was a bit confused after watching the video, but you explain interface concept very well to clear my confusion.
Erhan Kılıç
11,024 PointsErhan Kılıç
11,024 PointsMan thanks... I didn't understand at video but i understood with your example.
theay8
3,616 Pointstheay8
3,616 PointsThank-you, I also understood the purpose of interfaces much more clearly after reading your comment!
sayedshahidi
9,158 Pointssayedshahidi
9,158 PointsThanks !
Zeshan Ahmed
13,151 PointsZeshan Ahmed
13,151 Pointsamazing... cleared now!
Jaime Rios
Courses Plus Student 21,100 PointsJaime Rios
Courses Plus Student 21,100 PointsThanks so much. I find this example better than the one in the video.