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:
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 chooses to take on all the abstract methods and constants.
For example, let's say we have an interface called Human as below:
Notice that an interface does not have method bodies. Instead, we simply declare what methods we need for something to be "Human", and we leave them blank. Each implementation of Human can choose how they want to approach the eat, walk and pee methods.
Here's an example:
I am implementing Human (that is, accepting its three requirements) in the class King:
In other words, King is saying that, first and foremost, he is Human.
When you implement an interface in a Class, you must implement (or finish) everything you have stated in the interface. That means the King cannot remove the pee() method from his Class because he is, after all, only human.
You can add as many Interfaces to King as you want, such as "Male" and "Royalty". Then, you can use the King object in any method that requires a King or Human or Male or Royalty input, such as:
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 abstract qualities of the interface and implement it into 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.
In inheritance, we take an abstract concept, like Phone, and create a class which will hold all the shared properties of all Phones. Then, we can use this as a blueprint to create more specialized objects, such as the iPhone class or the GalaxyS class.
Let's have a look at an example:
Now the class iPhone will be able to use all the methods and variables defined in the superclass Phone... and add its own methods (like adjustPrice())
Polymorphism also works with inheritance.
Let's say we have a Hammer class:
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