The Artima Developer Community
Sponsored Link

Objects and Java Seminar
Polymorphism and Interfaces
Lecture Handout

Agenda


Polymorphism Gives You Substitutability


Polymorphism Gives You Extensibility


Interface and Implementation


Upcasting


Invoking Methods


Static Binding


Dynamic Binding


Polymorphism Example


Inheritance and Polymorphism


Abstract Classes and Methods


Interfaces


Implementing Interfaces


Interfaces Can "Extend" Other Interfaces


Classes Can Implement Multiple Interfaces


Exercises

Problem 1.

Take the code from Problem 4 from the Composition and Inheritance exercises and replace class Rodent with an interface. To refresh your memory, here's Problem 4 from Composition and Inheritance:

Create an inheritance hierarchy of Rodent: Mouse, Gerbil, Hamster, etc. In the base class, provide methods that are common to all Rodents, and override these in the derived classes to perform different behaviors depending on the specific type of Rodents. Create an array of Rodent, fill it with different specific types of Rodents, and call your base-class methods to see what happens.

Problem 2.

In a class named Dictionary (in a file named Dictionary.java), create a class (static) variable named WORD_COUNT of type long. Make WORD_COUNT public and final. Initialize WORD_COUNT in a static initializer to the value 1234567. (A static initializer is just an "= <value>" after the declaration of the static variable and before the semicolon. Create a static initialization block inside Dictionary that prints the value of WORD_COUNT out to the standard output with this string message: "Dictionary: WORD_COUNT == <value>".

Create a class named SpellChecker (in a file named SpellChecker.java) that has a main() method of the usual signature (public, static, void, a String array as its only parameter). In the main() method, print the value of Dictionary.WORD_COUNT to the standard output with this message: "SpellChecker: WORD_COUNT == <value>".

Compile these classes and run them to see the output. Take a moment to ponder the deep significance of this output.

Problem 3.

In class SpellChecker from Problem 2, create a new Dictionary object with the new keyword. Place this new statement first in the main method. Store the reference returned from new in a local variable.

Compile these classes again and run them to see the output. Why is this output different from that of Problem 2?

Problem 4.

Since you've been having so much fun with Dictionary and SpellChecker, please return to this code one last time. Take the classes from Problem 3 and edit Dictionary.java. Change the value of WORD_COUNT in Dictionary to 7654321. Recompile just Dictionary.java. It is important that you compile Dictionary.java without recompiling SpellChecker.java. Make sure you type:

javac Dictionary.java

Run the SpellChecker program one last time and observe its startling output. What went wrong?

Problem 5.

In the PolymorphInt/examples/ex1 directory of the sample code, create a class Tea that extends Liquid. Define a swirl() method in Tea that overrides Liquid's implementation. In the body of Tea's swirl() method, just print "Swirling Tea" to the standard output.

Edit Example1.java in the PolymorphInt/examples/ex1 directory. At the end of the main() method, add two more statements. In the first statement, create a new Tea instance and store the reference in a local variable. In the second statement, create a new Cup instance, passing a reference to the Tea object to Cup's constructor. Execute the Example1 application and observe the results.

Problem 6.

In the PolymorphInt/examples/ex5 directory, create a new class Puppy. Make Puppy implement Bubblebathable, but don't actually implement any methods in the body of Puppy (just declare that "Puppy implements Bubblebathable". Attempt to compile your program and observe the error messages generated by the compiler.

Problem 7.

In the Puppy.java class from Problem 6, implement whatever methods you need to implement to get class Puppy to compile. In each method, just print out a message to the standard output saying which method is being called, such as "Puppy: wash() invoked.".

Type in this Problem7 application and run it:

class Problem7 {

    public static void main(String[] args) {

        Puppy puppy = new Puppy();
        soakIt(puppy);
    }

    public static void soakIt(Soakable s) {
        s.wash();
        s.soak();
    }
}

Problem 8.

Copy Problem7.java to Problem8.java, and rename the class it contains Problem8. Changing only the body of the soakIt() method in class Problem8, find a way to invoke, in addition to wash() and soak(), the takeABubbleBath() method on any passed object that happens to implement BubbleBathable. (Do not change the type of the passed s parameter. Your revised soakIt() method should also accept a Soakable reference.) Run the Problem8 program and observe the output.


Sponsored Links

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use