From the course: Java 8+ Essential Training: Objects and APIs

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

Extend classes and override methods - Java Tutorial

From the course: Java 8+ Essential Training: Objects and APIs

Start my 1-month free trial

Extend classes and override methods

- [Narrator] In Java, you create a subclass of an existing class with the key word extends. Each class in Java can have one and only one super class. If you don't define an explicit super class then your custom class extends the Java object class. I'm going to start by creating a new custom class. It's going to hold some data. And in Java, there's a convention that says, this kind of class is sometimes known as a model class. I'll organize my code by putting these kinds of classes in their own package. From the base package of the application, I'll create a new package, and I'll name it model. And then I'll create a new Java class that's in that package. And I'll name it ClothingItem. This is going to become a super class. I'm creating an inheritance hierarchy, where I have a clothing item class, and then it can have one or more sub-classes for each type of clothing I want to sell. All clothing items have a certain amount of common functionality. Each item has a particular type, each…

Contents