Picture
Welcome to Day 10, Unit 1's penultimate lesson! 

This is the beginning of the end of the Basic Java unit.
With that said, you will still be learning Java throughout the future Units, so keep your minds open and ready to learn!

In this lesson, we will be discussing inheritance and interfaces (not in extensive detail, as they will come up frequently in future Units and it is easiest to understand them as you manipulate them yourself). 

Since this lesson is the culmination of the previous lessons in the Unit, we will be applying much of what we learned previously.

If you need to refresh your memory, refer to the previous lessons!

Let's get started!

Lesson #1-19: Interfaces

If you've ever seen highly functional Java code, you might notice something like this in the class declaration: 
Picture
This line of code creates a class named ClassName and implements the interface named InterfaceName.

Yeah, that's quite a mouthful... so you can think of it this way.

An interface is a collection of abstract (meaning that it's not really used, but it's just there as a reference) methods and constants that define a group. 

If a class implements this interface, then it will "inherit" all the abstract methods and constants.

For example, let's say we have an interface called Human as below: 
Picture
And we also have a class named King:
Picture
When class King implements interface Human, the class is basically applying every abstract quality of the interface Human onto itself.
In other words, King is saying that, first and foremost, he is Human.

When you implement an interface in a Class, you must define everything you have defined in the interface. That means the King cannot remove the pee() method from his Class because he is, after all, only human.

Lesson #1-20: Inheritance

This is a very similar topic to interfaces.

The key difference is that inheritance deals with two classes, not a class and an interface.
Another key difference is that inheritance uses the keyword "extends" rather than "implements."

So how is "extends" different from "implements?"

Recall that when you implement an interface, you must take all the qualities of the interface and apply it to the Class that is implementing it.
Not so when a class extends another class, which we refer to as a Superclass, and the extending class is called a subclass.

A class is a blueprint for objects, as I have repeated over and over again.

A superclass is a blueprint for other classes.

Let's have a look at an example: 
Picture
Using this superclass, we can create a subclass, which will (this is key) ADD-ON to the methods described in the superclass: 
Picture
Now the class iPhone will be able to use all the methods and constants defined in the superclass Phone... and add its own methods (like adjustPrice())  ;)

Not an extremely long lesson, but this is a very important one. Read through it thoroughly!
One more thing...

If each of you told your friends about this tutorial and asked them to like us on Facebook, it would help us out tremendously. 

Take the time to support Kilobolt!  :D
Picture
 


Comments

taoshi link
11/05/2012 7:37pm

Great site!!! Im learning Java in college but Im want to be a game developer on my own and this site is just out of the planet XD
I've download Tumbl in my SGA already ;) and I'll share this site with the students.

Keep it up!

Reply
James C.
11/07/2012 12:42am

Thank you so much! :)

Reply
Will
11/08/2012 8:34pm

When you said in Day 10: "When you implement an interface in a Class, you must define everything you have defined in the interface. That means the King cannot remove the pee() method from his Class because he is, after all, only human."

It doesn't mean that you have to implement every method or function, right? I'm use to program for .NET so Java is not new in a way but still just got to ask in this one :) and GREAT lessons, very simple and even expert developers can get lots of things from this posts

Reply
James C.
11/08/2012 9:36pm

If you do not implement it, you will get an error saying that you must implement unimplemented methods.

You could always call the superclass' method in these implementations, however.

Reply
Reece
11/17/2012 1:18am

Another good lesson. Thank you!

Reply
Justin
11/25/2012 3:51pm

if (hasAppleLogo == true){

price += 450 // LOL!!

}

// I'd like to chip in a few lines...

else if ((hasAppleLogo == true) && (isSModel == true)){

price += 650

}

else{

System.out.print.ln("This phone is reasonably priced and thus must not be an iPhone")

}

Reply
Chris Vetrano
01/13/2013 3:24pm

I wish you explained more thouroughly how we could construct this using eclipse and mess around with classes, super classes and implements...

Otherwise, this whole tutorial has been great!

Reply
magento website developer link
07/11/2013 4:10am

I think that your viewpoint regarding "Java developer" is deep, it’s just well thought out and truly incredible to see someone who knows how to put these thoughts so well. Good job I read your post and I found it amazing .Your thought process is wonderful.

Reply
DVendy
02/23/2013 6:55am

if (hasAppleLogo == true){

price += 450

}

//I see what you did there., :D

Reply
Djiango
03/07/2013 3:29am

How do I call these? How do I get it to print out "The king eats"?
Same for the Iphone example, let's say I put a print in at the adjustPrice method, how do I make it run and print the output!
What to write in the main callback?

Reply
Joshua Ospina
03/26/2013 11:14am

Hey. Keep up the good work. I just can't wait to make my game.

P.S. I am still paying attention to your lessons.

Reply
James Anderson
04/15/2013 1:52am

Why you put [] after args instead of after String in main method?

Reply
James C.
04/21/2013 7:16pm

It does the same thing.

Reply
Professional wordpress website developer link
07/11/2013 4:07am

I am very glad that I find your regular post "Website developer". Which seems to be very important and it made good time pass for me. I will always give a nice thrust look in to you from my bookmark feed. I don’t actually comment and don’t like to spend time in typing the comment.

Reply



Leave a Reply

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge.