|
You could potentially obtain more precise type checking, but only by sacrificing polymorphism and duplicating code. You could, for instance, create a special List class (with corresponding Cons and Empty subclasses, as well as a special Visitor interface) for each class of element you store in a List. In the example above, you would create an IntegerList class whose elements are all Integers. But if you wanted to store, say, Booleans in some other place in the program, you would have to create a BooleanList class. In my previous Java Tip, I discussed how you can get your compiler to check that constant values are valid. As I mentioned in that tip, although the use of these constructs makes code more readable, it can lead to a series of if/else if clauses in the client code. This new Java Tip also incorporates typesafe constants and a technique that can be used to remove the aforementioned condition checking. Polymorphism in Java is invariably subtype polymorphism. Closely examining the mechanisms that generate that variety of polymorphic behavior requires that we discard our usual implementation concerns and think in terms of type. This article investigates a type-oriented perspective of objects, and how that perspective separates what behavior an object can express from how the object actually expresses that behavior. By freeing our concept of polymorphism from the implementation hierarchy, we also discover how Java interfaces facilitate polymorphic behavior across groups of objects that share no implementation code at all. This is polymorphism in its purest form. By creating different implementations of DateFormat but treating each of them as the superclass, we have a type that displays many different behaviors. By taking such an approach, we can program very generically. 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 "= The creation of if-else constructs like the one shown above are possible in Java because of the instanceof operator, which allows you to check whether an object is an instance of a certain class. Here the instanceof operator is being abused, and the code should be reorganized to use polymorphism. The instanceof operator, and the situations in which it should be used, will be discussed later in this chapter. Use getPlace() as an example of instanceof. Use perhaps values() and keySet() of LinkMap as example of polymorphism and dynamic binding. Previous article: The Essence of OOP Using Java, Polymorphism and the Object Class Next article: The Essence of OOP using Java, Polymorphism and Interfaces, Part 2 The first lesson in the group was entitled The Essence of OOP Using Java, Objects, and Encapsulation. That lesson, and each of the lessons following that one, has provided explanations of certain aspects of the essence of Object-Oriented Programming using Java. The previous lesson was entitled The Essence of OOP using Java, Polymorphism and Interfaces, Part 1. As you gain more experience with Java, you will learn that much of the power of OOP using Java is centered on runtime polymorphism using class inheritance, interfaces, and method overriding. (The use of interfaces for polymorphism will be discussed in a subsequent lesson.) Previous article: The Essence of OOP Using Java, Inheritance, Part 2 Next article: The Essence of OOP using Java, Polymorphism, Type Conversion, Casting, Etc. The first lesson in the group was entitled The Essence of OOP Using Java, Objects, and Encapsulation. That lesson, and each of the lessons following that one, has provided explanations of certain aspects of the essence of Object-Oriented Programming using Java. The previous lesson was entitled The Essence of OOP using Java, Runtime Polymorphism through Inheritance. The first lesson in the group was entitled The Essence of OOP Using Java, Objects, and Encapsulation. That lesson, and each of the lessons following that one, has provided explanations of certain aspects of the essence of Object-Oriented Programming using Java. The previous lesson was entitled The Essence of OOP using Java, Polymorphism Based on Overloaded Methods. |
w___w_w__.j_av___a2s___.c__o___m___ | Contact Us |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |