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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
What is polymorphism?1m 22s
-
(Locked)
Writing reusable algorithms with runtime polymorphism4m 47s
-
(Locked)
Exploring different forms in a single object4m 6s
-
(Locked)
Accessing specific object forms with instanceof4m 23s
-
(Locked)
Writing flexible code with compile-time polymorphism4m 38s
-
(Locked)
Exploring how built-in Java classes use polymorphism3m 39s
-
(Locked)
Challenge: Reduce complexity with polymorphism48s
-
(Locked)
Solution: Reduce complexity with polymorphism6m 57s
-
(Locked)
-
-