Mark As Completed Discussion

Polymorphism

The last of the four fundamental properties of OOP is polymorphism. Polymorphism is a property that says all objects must act like all its parent types from which it is inherited. It can also act like other types (e.g., interfaces). In this section, we will discuss only the first part and leave interfaces for another lesson.

Polymorphism

Let's take our Shape class example and build a program with user inputs. In our console program, the user will select a number of different shapes and input the necessary properties of those shapes. Our program will then calculate the total area of all the shapes and output the sum!

The first thing we need for each shape is a method that takes values from the user. Below is the code for it. Notice that we are using an annotation @Override to override a method. This helps the IDE to identify your mistake if you accidentally create a new method instead of overriding an existing method (e.g. change parameter order or type).

JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment