From the course: Java Object-Oriented Programming

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Writing reusable algorithms with runtime polymorphism

Writing reusable algorithms with runtime polymorphism - Java Tutorial

From the course: Java Object-Oriented Programming

Writing reusable algorithms with runtime polymorphism

- [Instructor] Let's explore how to implement polymorphism in Java. We'll look at a similar example to our ModArrayList but this time, we'll create an OddArrayList that will only contain odd numbers. We'll want it to have all of the functionality of an ArrayList. So we'll extends ArrayList. We also know it will only contain odd whole numbers, so we can have it extended with the integer data type. In order to make it so that only odd numbers can be added to the list, we'll need to override some of the ArrayList functionality. Methods like the constructor, add, addAll, set, and replaceAll will need to be modified so that only odd numbers are added. To override the function implementations, we'll need to match the method signatures from the ArrayList exactly. We can still use the original functionality with the keyword super to access the original implementation. Let's take a look at how this works. To override the add…

Contents