Thinking in Java Polymorphism is the third essential feature of an object-oriented programming language, after data abstraction and inheritance.
It provides another dimension of separation of interface from implementation, to decouple what from how. Polymorphism allows improved code organization and readability as well as the creation of extensible programs that can be “grown” not only during the original creation of the project, but also when new features are desired.33220
Encapsulation creates new data types by combining characteristics and behaviors. Implementation hiding separates the interface from the implementation by making the details private. This sort of mechanical organization makes ready sense to someone with a procedural programming background. But polymorphism deals with decoupling in terms of types. In the last chapter, you saw how inheritance allows the treatment of an object as its own type or its base type. This ability is critical because it allows many types (derived from the same base type) to be treated as if they were one type, and a single piece of code to work on all those different types equally. The polymorphic method call allows one type to express its distinction from another, similar type, as long as they’re both derived from the same base type. This distinction is expressed through differences in behavior of the methods that you can call through the base class.
In this chapter, you’ll learn about polymorphism (also called dynamic binding or late binding or run-time binding) starting from the basics, with simple examples that strip away everything but the polymorphic behavior of the program.
In Chapter 6 you saw how an object can be used as its own type or as an object of its base type. Taking an object reference and treating it as a reference to its base type is called upcasting because of the way inheritance trees are drawn with the base class at the top.
You also saw a problem arise, which is embodied in the following example about musical instruments. Since several examples play Notes, we should create the Note class separately, in a package.
This is an “enumeration” class, which has a fixed number of constant objects to choose from. You can’t make additional objects because the constructor is private.
In the following example, Wind is a type of Instrument, therefore Wind is inherited from Instrument.
The method Music.tune() accepts an Instrument reference, but also anything derived from Instrument. In main(), you can see this happening as a Wind reference is passed to tune(), with no cast necessary. This is acceptable—the interface in Instrument must exist in Wind, because Wind is inherited from Instrument. Upcasting from Wind to Instrument may “narrow” that interface, but it cannot make it anything less than the full interface to Instrument.
Forgetting the object type, Music.java might seem strange to you. Why should anyone intentionally forget the type of an object? This is what happens when you upcast, and it seems like it could be much more straightforward if tune() simply takes a Wind reference as its argument. This brings up an essential point: If you did that, you’d need to write a new tune() for every type of Instrument in your system. Suppose we follow this reasoning and add Stringed and Brass instruments.
This works, but there’s a major drawback: you must write type-specific methods for each new Instrument class you add. This means more programming in the first place, but it also means that if you want to add a new method like tune( ) or a new type of Instrument, you’ve got a lot of work to do. Add the fact that the compiler won’t give you any error messages if you forget to overload one of your methods and the whole process of working with types becomes unmanageable.
Wouldn’t it be much nicer if you could just write a single method that takes the base class as its argument, and not any of the specific derived classes? That is, wouldn’t it be nice if you could forget that there are derived classes, and write your code to talk only to the base class?
- 上一篇:电子商务英文文献和中文翻译
- 下一篇:搅拌釜内混合液体的分离涡模拟英文文献和中文翻译
-
-
-
-
-
-
-
现代简约美式风格在室内家装中的运用
高警觉工作人群的元情绪...
NFC协议物理层的软件实现+文献综述
上市公司股权结构对经营绩效的影响研究
浅析中国古代宗法制度
巴金《激流三部曲》高觉新的悲剧命运
C++最短路径算法研究和程序设计
g-C3N4光催化剂的制备和光催化性能研究
江苏省某高中学生体质现状的调查研究
中国传统元素在游戏角色...