From the course: Java 11+ Essential Training

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Use objects as their super types

Use objects as their super types - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Use objects as their super types

- [Instructor] In object oriented programming, the term polymorphism refers to the ability to treat an object as either its own type or as its super type. I'll demonstrate this using my inheritance hierarchy that I created in previous exercises. I have a super class named ClothingItem and it has a single subclass named SHIRT. I'm going to add another subclass of clothing item. In the project window, I'll make a copy of the shirt and then I'll go to the model package and paste it and I'll name the new one HAT. When I create the new class the constructor is updated automatically because the constructor method's name must match the class's name. But the type isn't right yet. I'm referencing the constant ClothingItem.SHIRT. I'll go back to my ClothingItem class and I'll add a new constant that I'll name HAT and I'll set that value using a string that I want the user to see. Then I'll come back to Hat.java and change the type…

Contents