Join David Gassner for an in-depth discussion in this video Extending classes and overriding methods, part of Java 8 Essential Training.
- So now I'll demonstrate creating and using subclasses of a super class. I'm working on a new version of my project named ExtendingClasses. And I've refactored a little bit. Notice in the main class that instead of passing in a long value as the color, I'm instead using a new enumeration named OliveColor. To jump to the enum, I'll hold down the Control key on Windows or the Command key on Mac, and click the name of the enum. And that opens that file and jumps to the declaration.
This enum has three options: Purple, black, and green. And they each have associated names and long color values. The constructor method receives both values and saves them, and the two-string method returns just the name. So now my goal is to refactor my code. Instead of passing in an instance of Olive, and explicitly setting the name, color, and amount of oil each time, I'll have a special subclass for each type of Olive. And that subclass will already know those values.
I'll create these subclasses in the model package. I'll right-click on the model package and choose New, Java Class. And I'll set the name of the class to Kalamata. And click OK. Now to make this a subclass of Olive, I'll add the extends key word and then select the Olive class. Now I'll get rid of this comment. And notice initially that I have an error. It tells me there is no default constructor available in the Olive class.
And that's because I removed it. By default the sublcass is expecting there to be a no argument superclass in the subclass. But, it doesn't exist. So I need to create it explicitly here. I'll create a constructor specifically for this subclass. And it'll receive no arguments. And that makes the error go away. But now, I have a new error. It's also telling me there's no default constructor available. But that's okay, because I want to call the version of the constructor that accepts three arguments.
And here's how you do it. Use the keyword super. And this is actually a method call. The method is expecting an Olive name, an Olive color, and an amount of oil. So, I'll pass in OliveName.KALAMATA. Then I'll pass in a color, OliveColor.PURPLE. And an amount of oil, and I'll say that all Kalamata Olives return two units of oil. And that gets rid of all of my errors. Now I'll go back to Main.Java.
Now, instead of creating an instance of the Olive class, I can get rid of this bit of code, and this time I'll just say I want a Kalamata Olive. And the Kalamata Olive already knows all those values. I'll get rid of the second version of this. And then duplicate the first, and now I have two Kalamata Olives. Now, I'll go through the same process for Ligurian. I'll go to the Kalamata class that I just created.
And I'll right-click and copy it to the clipboard. Then I'll right-click on the model package, and I'll paste. IntelliJ IDEA asks me what's the new name of the class. And I'll say it's Ligurian. And that creates a new class named Ligurian that extends the Olive class just like Kalamata. But now it's up to me to change the values I'm passing in. I'll change the name to LIGURIAN. And I'll change the color to BLACK.
And again we've been saying that Ligurian is a particularly large Olive, so I'll set the amount of oil to 5. Then I'll come back to the main class, and I'll do the same sort of refactoring I did before. Instead of creating an instance of Olive, I'll create an instance of Ligurian. I'll get rid of these three lines of code. And then I'll duplicate Ligurian three times. And you can see how much simpler the code is and how much easier it is to read.
Now later on if I decide I was wrong, and Ligurian Olives get a smaller amount of oil, I can change it once here. Before I test, I'll look at my Problems View. To verify I have no syntax errors. Then I'll return to Packages, and I'll run the code. And there's the result. I'm getting two units from each Kalamata and five units from each Ligurian, and a total of 24 units of oil. I recommend reviewing the code for this entire application.
Because there are a few other minor changes that I made. But the important thing to learn here, is that the subclass can contain all sort of specific information and override the defaults or set specific values that a superclass requires. But it can still use the features of the superclass where it doesn't need to have specific functionality.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files