Bummer! You have been redirected as the page you requested could not be found.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Interfaces in Java!
You have completed Interfaces in Java!
Preview
In this video we'll look at a couple other ways interfaces help us to write good code!
Code to Copy
// Create Objects
ShopKeeper shopKeeper = new ShopKeeper("Larry");
Teacup teacup = new Teacup();
String treehouse = "Treehouse";
// Loop through Objects
Object[] objects = {shopKeeper, teacup, treehouse};
for (Object object : objects) {
if (object instanceof Chattable) {
String response = ((Chattable) object).chat();
System.out.println(response);
}
}
// Loop through Chattables
Chattable[] chattables = {shopKeeper, teacup};
for (Chattable chattable : chattables) {
String response = chattable.chat();
System.out.println(response);
}
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
look at a couple concrete examples.
0:00
Let's start by deleting the comment.
0:01
And then let's copy and paste in
some code from the teacher's notes.
0:05
And take a second to
get the spacing right.
0:10
Great, let's look through this code,
starting from the top.
0:16
The first thing we do is
create a few objects.
0:20
We've got a ShopKeeper named Larry, a
TeaCup, and a String that says treehouse.
0:23
Then we create an object array containing
all of the objects we just created.
0:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up