From the course: Nail Your Java Interview

Unlock the full course today

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

Use polymorphism to reduce complexity in coding interviews

Use polymorphism to reduce complexity in coding interviews - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Use polymorphism to reduce complexity in coding interviews

- [Instructor] Polymorphism is the ability for an object or function to take many forms. Depending on the context and the situation, the form may be different making your code more flexible and reducing complexity. Java supports two types of polymorphism, runtime, compile-time. First, we'll be looking at runtime polymorphism and we've already seen it quite a bit in this course even if you didn't realize it. Let's take a look. With our non-negative ArrayList example, we extended the ArrayList class. This makes the non-negative ArrayList be a non-negative ArrayList object as well as an ArrayList object. It takes these two forms. In our main function, we created an ArrayList of this class and added integers to the list. We also have a regular ArrayList and use the same method on it. Let's run it. Here we see that only non-negative items are added to the non-negative ArrayList and all the items are added to the…

Contents