From the course: Java 11+ Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Extend classes and override methods - Java Tutorial
From the course: Java 11+ Essential Training
Extend classes and override methods
- [David] Java's single inheritance model means that you can create subclasses of your own superclasses. I'll demonstrate this using my ClothingItem class that I'll treat as a superclass. I'll go to the model package in my application, and I'll create a new Java class. I'll set the name of the class as Shirt. I'll select the type of file as class and press enter. And that creates the new file. Now, I want to extend the ClothingItem class. I'll use the extends keyword, and choose that class. The Shirt class now inherits all of the functionality of ClothingItem. That means that it has access to the constructor method and to all of the setters and getters. It does not have direct access to the private fields. Those are still going to be managed within the superclass. Notice that I have an error condition here. It tells me that there is no default constructor available in the superclass. I'll fix this issue by adding an…