Comment on page
11.6 Exercises
- 1.Which of the following are examples of subtype polymorphism?
- Having a
sqrt(int)
andsqrt(double)
method in the same class. - Having a
Dog
override themakeSound
method that it inherits fromAnimal
. - Creating a class that implements the built-in
Comparable
interface.
- 2.How would you compare two strings alphabetically in Java?
- 3.Suppose we correctly define a
Comparator
class calledSixComparator
for integers that compares them based on the number of6
s they contain. What will the code(new SixComparator()).compare(12345678, 45666678)
return? - 4.What is the main difference between the
Comparable
andComparator
interfaces?
Having a
Dog
override the makeSound
method that it inherits from Animal
.Overriding
makeSound
allows us to have different implementations of the same method via subtypes.Creating a class that implements the built-in
Comparable
interface.Implementing the Comparable interface involves overriding Comparables’ methods, allowing for differing behavior of the same method across types.
Last modified 9mo ago