From the course: Java 8 Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Extending classes and overriding methods - Java Tutorial
From the course: Java 8 Essential Training
Extending classes and overriding methods
- 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.