Fri, May 31, 2019 4:50:42 PM

<br/>
Of course, once you see this design it becomes clear that the base class �cooling system� is not <br/>
general enough, and should be renamed to �temperature control system� so that it can also <br/>
include heating�at which point the substitution principle will work. However, this diagram <br/>
is an example of what can happen with design in the real world. <br/>
When you see the substitution principle it�s easy to feel like this approach (pure substitution) <br/>
is the only way to do things, and in fact it is nice if your design works out that way. But you�ll <br/>
find that there are times when it�s equally clear that you must add new methods to the <br/>
interface of a derived class. With inspection both cases should be reasonably obvious. <br/>
Interchangeable objects <br/>
with polymorphism <br/>
When dealing with type hierarchies, you often want to treat an object not as the specific type <br/>
that it is, but instead as its base type. This allows you to write code that doesn�t depend on <br/>
specific types. In the shape example, methods manipulate generic shapes, unconcerned about <br/>
whether they�re circles, squares, triangles, or some shape that hasn�t even been defined yet. <br/>
All shapes can be drawn, erased, and moved, so these methods simply send a message to a <br/>
shape object; they don�t worry about how the object copes with the message. <br/>
Such code is unaffected by the addition of new types, and adding new types is the most <br/>
common way to extend an object-oriented program to handle new situations. For example, <br/>
you can derive a new subtype of shape called pentagon without modifying the methods that <br/>
deal only with generic shapes. This ability to easily extend a design by deriving new subtypes <br/>
is one of the essential ways to encapsulate change. This greatly improves designs while <br/>
reducing the cost of software maintenance. <br/>
There�s a problem, however, with attempting to treat derived-type objects as their generic <br/>
base types (circles as shapes, bicycles as vehicles, cormorants as birds, etc.). If a method is <br/>
going to tell a generic shape to draw itself, or a generic vehicle to steer, or a generic bird to <br/>
move, the compiler cannot know at compile time precisely what piece of code will be <br/>
executed. That�s the whole point�when the message is sent, the programmer doesn�t want to <br/>
know what piece of code will be executed; the draw method can be applied equally to a circle, <br/>
a square, or a triangle, and the object will execute the proper code depending on its specific <br/>
type. <br/>
If you don�t have to know what piece of code will be executed, then when you add a new <br/>
subtype, the code it executes can be different without requiring changes to the method that <br/>
Introduction to Objects 25�<br/>
calls it. Therefore, the compiler cannot know precisely what piece of code is executed, so what <br/>
does it do? <br/>
For example, in the following diagram the BirdController object just works with generic <br/>
Bird objects and does not know what exact type they are. This is convenient from <br/>
BirdController�s perspective because it doesn�t have to write special code to determine the <br/>
exact type of Bird it�s working with or that Bird�s behavior. So how does it happen that, <br/>
when move( ) is called while ignoring the specific type of Bird, the right behavior will occur <br/>
(a Goose walks, flies, or swims, and a Penguin walks or swims)? <br/>
<br/>
The answer is the primary twist in object-oriented programming: The compiler cannot make <br/>
a function call in the traditional sense. The function call generated by a non-OOP compiler <br/>
causes what is called early binding, a term you may not have heard before because you�ve <br/>
never thought about it any other way. It means the compiler generates a call to a specific <br/>
function name, and the runtime system resolves this call to the absolute address of the code <br/>
to be executed. In OOP, the program cannot determine the address of the code until run <br/>
time, so some other scheme is necessary when a message is sent to a generic object. <br/>
To solve the problem, object-oriented languages use the concept of late binding. When you <br/>
send a message to an object, the code being called isn�t determined until run time. The <br/>
compiler does ensure that the method exists and performs type checking on the arguments <br/>
and return value, but it doesn�t know the exact code to execute. <br/>
To perform late binding, Java uses a special bit of code in lieu of the absolute call. This code <br/>
calculates the address of the method body, using information stored in the object (this <br/>
process is covered in great detail in the Polymorphism chapter). Thus, each object can <br/>
behave differently according to the contents of that special bit of code. When you send a <br/>
message to an object, the object actually does figure out what to do with that message. <br/>
In some languages you must explicitly state that you want a method to have the flexibility of <br/>
latebinding properties (C++ uses the virtual keyword to do this). In these languages, by <br/>
default, methods are not dynamically bound. In Java, dynamic binding is the default <br/>
behavior and you don�t need to remember to add any extra keywords in order to get <br/>
polymorphism. <br/>
Consider the shape example. The family of classes (all based on the same uniform interface) <br/>
was diagrammed earlier in this chapter. To demonstrate polymorphism, we want to write a <br/>
single piece of code that ignores the specific details of type and talks only to the base class. <br/>
That code is decoupled from type-specific information and thus is simpler to write and easier <br/>
to understand. And, if a new type�a Hexagon, for example�is added through inheritance, <br/>
the code you write will work just as well for the new type of Shape as it did on the existing <br/>
types. Thus, the program is extensible. <br/>
26 Venkata Pilaka Venkata Pilaka <br/>

Comments

Popular posts from this blog

termux vnc viewer setup

../Settings.jpg

me.html