From the course: Java Object-Oriented Programming

Unlock the full course today

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

Exploring different forms in a single object

Exploring different forms in a single object - Java Tutorial

From the course: Java Object-Oriented Programming

Exploring different forms in a single object

- [Teacher] Depending on the context, a given instance can take many different forms. With the odd listy, it takes the form of an odd ArrayList when the add method is used, only adding odd numbers. The other list, listy, takes the form of an ArrayList, adding all of the numbers to the list. Now what happens when we use the size method on both of these lists? Both of these take the form of an ArrayList since Odd ArrayList doesn't have a custom implementation for the size. We can leverage this to create common functionality that can work with both types of lists. For example, let's say we wanted to create a universal function that adds a random number to any integer ArrayList regardless of if it's odd or regular. We can create a function called addRandomNumber and have its input be an ArrayList. To generate the random number, we'll use the random class to create a random instance. Then we'll use it in call nextInt.…

Contents