Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The goal of creating an inheritance relationship between classes is to extend or inherit functionality in the subclass. I am going to demonstrate this in my OlivePress application, working in a version of the project named extending custom In this application I've defined a custom class named Olive, it has two public class fields BLACK and GREEN. It has three public instance fields, two strings in a long, and it has a private instance field for oil, an integer.
It has a getter and a setter for the oil value and a couple of constructor methods and one custom crush method. Now, I'm going to create subclasses for the Olive class. My goal is to create classes that inherit most of the functionality of the superclass, but then set particular values that are unique to those classes. I'll place my subclasses in the same package as the superclass olive. I'll go to the package Explorer, I'll right click on the package and I'll select New> Class.
I'll name my first subclass, Kalamata, I'll set the superclass to my olive class. When you set the superclass you have to use the entire package and class name. You can't just name the class by its name. And this would be tough to type without making mistakes. So I'm going to click the browse button and let Eclipse do some of the work for me. In the superclass selection dialog I'll type Olive and I'll choose Olive and click okay and then click finish. And here is my subclass Kalamata.
Now I'll repeat that operation and create a second subclass, I'll right-click on the package, choose New > Class and I'll name this one, Ligurian another olive variety. Once again I'll browse for the superclass selecting Olive and I'll click Finish. I now have two subclasses of the Olive class. Now when I create my olives in the main application, I'd like to be specific about what kind of olives I'm creating. Right now, I'm calling the Olive constructor method three times and passing in values for the amount of oil that that Olive will generate.
Instead I'd like to say I want to create two Kalamata's and one Ligurian. All I need to do is use the appropriate constructor methods for these classes, but I can't do it quite yet. Even though the superclass has constructor methods. Constructor methods are not literally speaking members of the class. There are special kind of syntax and so they're not inherited by subclasses, each subclass needs its own explicit constructor that matches its name.
So I will go to the Kalamata class and fix the problem there first. I'll place the cursor inside the class declaration and right-click and I'll choose Source and then I'll let Eclipse do some of the work for me by choosing this option, Generates Constructors From Superclass. I'm asked which constructors I want to implement, the one with no arguments or the one with the integer argument or both. Because of the way I am designing this application I only want my subclasses to have no arguments constructors.
So I'll deselect the version that would receive an integer argument and click OK. So here's my new constructor method. There is a single line of code here named super. The super method is a way of calling the superclass's constructor method. I'll leave that in place for the moment and I'll just remove the TODO comment. I'll save those changes and go to the Ligurian class and repeat the process. Once again I'll right-click inside the Class Declaration and choose Source generate constructors from superclass, I'll deselect the constructor that receives an integer argument and click OK, and I'll remove the TODO comment.
Now I'm ready to use the subclasses in my main application. I'll go to main.java in the main application file I'm calling the constructor method for the superclass three times. Notice that I declared a variable typed as the superclass, but when I actually construct the objects I don't have to use the superclass constructor. I can instead use the subclass constructor, the native type of the object will then be the subclass, but it can masquerade as the superclass.
This is polymorphism at work. By declaring as the superclass, I can then say I've got a whole bunch of olives, but I don't have to be specific about what types of olives, but when I construct them I can be specific and affect the behavior. So I'll go to the first constructor method call and I'll change that to Kalamata, I'll type Kal and press Ctrl+ Spacebar and the rest of the class name is filled in for me by Eclipse. Now, I'll do something similar but the second one will be an instance of the Ligurian class.
I'll type Lig and press Ctrl + Spacebar, and Eclipse finds two classes that start with that string, I'll choose Ligurian and then finally, I'll change the third one and I once again use Kalamata, typing Kal and Ctrl + Spacebar. When you type Ctrl + Spacebar, you should get new import statements up at the top of your code for the new classes. Now, I've still got problems here right now my subclasses only have no arguments constructor methods. They don't have versions of the constructor method that can receive values.
When I move the cursor over the first constructor method call, I get a list of available quick fixes. What do you want to do Eclipse is asking me to match the constructor. I'll choose the first one, remove the argument and now that constructor method call matches what I have in the class. I'll do the same thing for Ligurian, removing the argument and then for the second version of Kalamata. I'll save my changes, and it will now appear as though everything is in good shape. I'll run the application, and you'll see that after running the first series of olives I get nine units of oil, and after the second 18.
Well, why is this happening? The reason is because in the superclass, in the oil declaration there is a default value of three and I am no longer doing anything to override that value. My goal is to have each of the subclasses decide for itself. How much oil do I generate, what's my color, what's my name and so on. So I'll go over to kalamata. java and I'll make those changes. First of all, going back to the superclass these three fields are public and so I can change them from directly within the subclass, I'll say this.name = Kalamata and this.flavor equals Grassy and this.
color == Olive.BLACK. Now to set the oil value once again I'll go back to the superclass, and show you that we provided a way of setting the oil value using this version of the constructor method which is public. So go back to Kalamata, and I'll pass in a value of two. So now all Kalamata olives will have the same oil value. When I save my changes, notice the error at the bottom. It tells me that if I'm going to call the superclass's constructor, that has to go at the top before any other statements.
So I am going to move that code up holding down Alt on Windows or Option on Mac and pressing the up arrow a few times, then I'll save my changes. Now I'll copy those lines of code to the clipboard, and go over to Ligurian and I'll paste that in getting rid of the super method call with no arguments. I'll change the oil value for Ligurian to five. We'll say that that's a very big olive, I'll change the name to Ligurian, I'll change the flavor to Yummy and I'll change the color to green.
So now each of the subclasses is responsible for setting its own field values, when I run the application, all I need to know is that I am creating these instances of these subclasses, all the specifics are determined by the subclasses themselves and when I run the application, it looks like I got the same values, nine units and 18 units but it turns out that that's because of the values and the particular combination of olives that I just selected. So I'll go back to Ligurian and I'm going to change the Ligurian so that it's a really tiny olive, it only gives me one unit of oil, I'll run the application again and this time the numbers change.
So you can see how creating an inheritance relationship allows you to inherit functionality from a superclass but then set specific values in the subclass that are respected by the rest of the application. From the main application's point of view, all it needs to know is these are the types of olives, all of the details are handled in the subclass code.
Get unlimited access to all courses for just $25/month.Become a member