SlideShare a Scribd company logo
1 of 5
Interchangeable objects
   with polymorphism
When dealing with type hierarchies, you often want to treat an object
not as the specific type that it is, but instead as its base type. This
allows you to write code that doesn’t depend on specific types. In the
shape example, methods manipulate generic shapes without respect to
whether they’re circles, squares, triangles, or some shape that hasn’t
even been defined yet. All shapes can be drawn, erased, and moved,
so these methods simply send a message to a shape object; they don’t
worry about how the object copes with the message. Feedback

Such code is unaffected by the addition of new types, and adding new
types is the most common way to extend an object-oriented program
to handle new situations. For example, you can derive a new subtype
of shape called pentagon without modifying the methods that deal only
with generic shapes. This ability to easily extend a design by deriving
new subtypes is one of the essential ways to encapsulate change. This
greatly improves designs while reducing the cost of software
maintenance. Feedback

There’s a problem, however, with attempting to treat derived-type
objects as their generic base types (circles as shapes, bicycles as
vehicles, cormorants as birds, etc.). If a method is going to tell a
generic shape to draw itself, or a generic vehicle to steer, or a generic
bird to move, the compiler cannot know at compile time precisely what
piece of code will be executed. That’s the whole point—when the
message is sent, the programmer doesn’t want to know what piece of
code will be executed; the draw method can be applied equally to a
circle, a square, or a triangle, and the object will execute the proper
code depending on its specific type. If you don’t have to know what
piece of code will be executed, then when you add a new subtype, the
code it executes can be different without requiring changes to the
method call. Therefore, the compiler cannot know precisely what piece
of code is executed, so what does it do? For example, in the following
diagram the BirdController object just works with generic Bird
objects and does not know what exact type they are. This is
convenient from BirdController’s perspective because it doesn’t have
to write special code to determine the exact type of Bird it’s working
with or that Bird’s behavior. So how does it happen that, when
move( ) is called while ignoring the specific type of Bird, the right
behavior will occur (a Goose runs, flies, or swims, and a Penguin
runs or swims)? Feedback
The answer is the primary twist in object-oriented programming: the
compiler cannot make a function call in the traditional sense. The
function call generated by a non-OOP compiler causes what is called
early binding, a term you may not have heard before because you’ve
never thought about it any other way. It means the compiler
generates a call to a specific function name and the linker resolves this
call to the absolute address of the code to be executed. In OOP, the
program cannot determine the address of the code until run time, so
some other scheme is necessary when a message is sent to a generic
object. Feedback

To solve the problem, object-oriented languages use the concept of
late binding. When you send a message to an object, the code being
called isn’t determined until run time. The compiler does ensure that
the method exists and performs type checking on the arguments and
return value (a language in which this isn’t true is called weakly
typed), but it doesn’t know the exact code to execute. Feedback

To perform late binding, Java uses a special bit of code in lieu of the
absolute call. This code calculates the address of the method body,
using information stored in the object (this process is covered in great
detail in Chapter 7). Thus, each object can behave differently
according to the contents of that special bit of code. When you send a
message to an object, the object actually does figure out what to do
with that message. Feedback

In some languages you must explicitly state that you want a method
to have the flexibility of late-binding properties (C++ uses the virtual
keyword to do this). In these languages, by default, methods are not
dynamically bound. In Java, dynamic binding is the default behavior
and you don’t need to remember to add any extra keywords in order
to get polymorphism. Feedback

Consider the shape example. The family of classes (all based on the
same uniform interface) was diagrammed earlier in this chapter. To
demonstrate polymorphism, we want to write a single piece of code
that ignores the specific details of type and talks only to the base
class. That code is decoupled from type-specific information and thus
is simpler to write and easier to understand. And, if a new type—a
Hexagon, for example—is added through inheritance, the code you
write will work just as well for the new type of Shape as it did on the
existing types. Thus, the program is extensible. Feedback

If you write a method in Java (as you will soon learn how to do):
Feedback


void doStuff(Shape s) {
  s.erase();
  // ...
  s.draw();
}




This method speaks to any Shape, so it is independent of the specific
type of object that it’s drawing and erasing. If some other part of the
program uses the doStuff( ) method: Feedback

Circle c = new Circle();
Triangle t = new Triangle();
Line l = new Line();
doStuff(c);
doStuff(t);
doStuff(l);




the calls to doStuff( ) automatically work correctly, regardless of the
exact type of the object. Feedback

This is a rather amazing trick. Consider the line:

doStuff(c);
What’s happening here is that a Circle is being passed into a method
that’s expecting a Shape. Since a Circle is a Shape it can be treated
as one by doStuff( ). That is, any message that doStuff( ) can send
to a Shape, a Circle can accept. So it is a completely safe and logical
thing to do. Feedback

We call this process of treating a derived type as though it were its
base type upcasting. The name cast is used in the sense of casting into
a mold and the up comes from the way the inheritance diagram is
typically arranged, with the base type at the top and the derived
classes fanning out downward. Thus, casting to a base type is moving
up the inheritance diagram: “upcasting.” Feedback




An object-oriented program contains some upcasting somewhere,
because that’s how you decouple yourself from knowing about the
exact type you’re working with. Look at the code in doStuff( ): Feedback

  s.erase();
  // ...
  s.draw();




Notice that it doesn’t say “If you’re a Circle, do this, if you’re a
Square, do that, etc.” If you write that kind of code, which checks for
all the possible types that a Shape can actually be, it’s messy and you
need to change it every time you add a new kind of Shape. Here, you
just say “You’re a shape, I know you can erase( ) and draw( )
yourself, do it, and take care of the details correctly.” Feedback

What’s impressive about the code in doStuff( ) is that, somehow, the
right thing happens. Calling draw( ) for Circle causes different code
to be executed than when calling draw( ) for a Square or a Line, but
when the draw( ) message is sent to an anonymous Shape, the
correct behavior occurs based on the actual type of the Shape. This is
amazing because, as mentioned earlier, when the Java compiler is
compiling the code for doStuff( ), it cannot know exactly what types
it is dealing with. So ordinarily, you’d expect it to end up calling the
version of erase( ) and draw( ) for the base class Shape, and not for
the specific Circle, Square, or Line. And yet the right thing happens
because of polymorphism. The compiler and run-time system handle
the details; all you need to know right now is that it does happen, and
more importantly, how to design with it. When you send a message to
an object, the object will do the right thing, even when upcasting is
involved. Feedback

Abstract base classes and interfaces

Often in a design, you want the base class to present only an interface
for its derived classes. That is, you don’t want anyone to actually
create an object of the base class, only to upcast to it so that its
interface can be used. This is accomplished by making that class
abstract by using the abstract keyword. If anyone tries to make an
object of an abstract class, the compiler prevents it. This is a tool to
enforce a particular design. Feedback

You can also use the abstract keyword to describe a method that
hasn’t been implemented yet—as a stub indicating “here is an
interface method for all types inherited from this class, but at this
point I don’t have any implementation for it.” An abstract method
may be created only inside an abstract class. When the class is
inherited, that method must be implemented, or the inheriting class
becomes abstract as well. Creating an abstract method allows you to
put a method in an interface without being forced to provide a possibly
meaningless body of code for that method. Feedback

The interface keyword takes the concept of an abstract class one
step further by preventing any method definitions at all. The interface
is a very handy and commonly used tool, as it provides the perfect
separation of interface and implementation. In addition, you can
combine many interfaces together, if you wish, whereas inheriting
from multiple regular classes or abstract classes is not possible.

More Related Content

What's hot

Coders club java tutorial
Coders club java tutorialCoders club java tutorial
Coders club java tutorialShivaum Kumar
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSergey Aganezov
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Anshul Vinayak
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basicsHang Dong
 
C#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language EnhancmentsC#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language Enhancmentstechfreak
 
E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3Vijay Perepa
 
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...Philip Schwarz
 
Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patternsGomathiNayagam S
 
Gof design pattern
Gof design patternGof design pattern
Gof design patternnaveen kumar
 
25 java tough interview questions
25 java tough interview questions25 java tough interview questions
25 java tough interview questionsArun Banotra
 
Baroda code smell and refactoring
Baroda  code smell and refactoringBaroda  code smell and refactoring
Baroda code smell and refactoringMamata Gelanee
 
Impact of indentation in programming
Impact of indentation in programmingImpact of indentation in programming
Impact of indentation in programmingijpla
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in Indiaibinstitute0
 
Introduction to Excel VBA/Macros
Introduction to Excel VBA/MacrosIntroduction to Excel VBA/Macros
Introduction to Excel VBA/Macrosarttan2001
 

What's hot (20)

Code Smells
Code SmellsCode Smells
Code Smells
 
Coders club java tutorial
Coders club java tutorialCoders club java tutorial
Coders club java tutorial
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basics
 
Creational Design Patterns
Creational Design PatternsCreational Design Patterns
Creational Design Patterns
 
C#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language EnhancmentsC#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language Enhancments
 
E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
 
Javascript design patterns
Javascript design patternsJavascript design patterns
Javascript design patterns
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
unit_tests_tutorial
unit_tests_tutorialunit_tests_tutorial
unit_tests_tutorial
 
Classes2
Classes2Classes2
Classes2
 
25 java tough interview questions
25 java tough interview questions25 java tough interview questions
25 java tough interview questions
 
Baroda code smell and refactoring
Baroda  code smell and refactoringBaroda  code smell and refactoring
Baroda code smell and refactoring
 
Impact of indentation in programming
Impact of indentation in programmingImpact of indentation in programming
Impact of indentation in programming
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
Introduction to Excel VBA/Macros
Introduction to Excel VBA/MacrosIntroduction to Excel VBA/Macros
Introduction to Excel VBA/Macros
 

Viewers also liked (17)

Tik bab 1 dasar dasar internet
Tik bab 1 dasar dasar internetTik bab 1 dasar dasar internet
Tik bab 1 dasar dasar internet
 
Nutrients
NutrientsNutrients
Nutrients
 
Rassegna stampa A&P 2015
Rassegna stampa A&P 2015Rassegna stampa A&P 2015
Rassegna stampa A&P 2015
 
Muscles
MusclesMuscles
Muscles
 
No chexsystems banks helping the unbanked
No chexsystems banks helping the unbankedNo chexsystems banks helping the unbanked
No chexsystems banks helping the unbanked
 
Edizioni precedenti 2011 - 2002
Edizioni precedenti 2011 - 2002Edizioni precedenti 2011 - 2002
Edizioni precedenti 2011 - 2002
 
Rebuild Credit Fast
Rebuild Credit FastRebuild Credit Fast
Rebuild Credit Fast
 
Food chain
Food chainFood chain
Food chain
 
Sajak anak palestin
Sajak anak palestinSajak anak palestin
Sajak anak palestin
 
Microbes
MicrobesMicrobes
Microbes
 
Edizioni precedenti
Edizioni precedentiEdizioni precedenti
Edizioni precedenti
 
Human skeletal system
Human skeletal systemHuman skeletal system
Human skeletal system
 
Tik bab 2 perangkat jaringan
Tik bab 2 perangkat jaringanTik bab 2 perangkat jaringan
Tik bab 2 perangkat jaringan
 
Human organ systems
Human organ systemsHuman organ systems
Human organ systems
 
Cerpen idola
Cerpen idolaCerpen idola
Cerpen idola
 
Lesson 2 Circulatory System - Grade 9
Lesson 2 Circulatory System - Grade 9Lesson 2 Circulatory System - Grade 9
Lesson 2 Circulatory System - Grade 9
 
Microorganisms
MicroorganismsMicroorganisms
Microorganisms
 

Similar to Abstract

GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural PatternsSameh Deabes
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your CodeRookieOne
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsLalit Kale
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAyush Sharma
 
Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?Andrey Karpov
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxdanhaley45372
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsSaurabh Narula
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsMichael Heron
 
Looking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelopLooking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelopPVS-Studio
 
C# classes
C#   classesC#   classes
C# classesTiago
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
Programming for a better world
Programming for a better worldProgramming for a better world
Programming for a better worldjhansi reddy
 
The advantage of developing with TypeScript
The advantage of developing with TypeScript The advantage of developing with TypeScript
The advantage of developing with TypeScript Corley S.r.l.
 

Similar to Abstract (20)

GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your Code
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?Still Comparing "this" Pointer to Null?
Still Comparing "this" Pointer to Null?
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design Patterns
 
Looking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelopLooking for Bugs in MonoDevelop
Looking for Bugs in MonoDevelop
 
C# classes
C#   classesC#   classes
C# classes
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Programming for a better world
Programming for a better worldProgramming for a better world
Programming for a better world
 
Oo ps exam answer2
Oo ps exam answer2Oo ps exam answer2
Oo ps exam answer2
 
The advantage of developing with TypeScript
The advantage of developing with TypeScript The advantage of developing with TypeScript
The advantage of developing with TypeScript
 
AngularConf2015
AngularConf2015AngularConf2015
AngularConf2015
 

Abstract

  • 1. Interchangeable objects with polymorphism When dealing with type hierarchies, you often want to treat an object not as the specific type that it is, but instead as its base type. This allows you to write code that doesn’t depend on specific types. In the shape example, methods manipulate generic shapes without respect to whether they’re circles, squares, triangles, or some shape that hasn’t even been defined yet. All shapes can be drawn, erased, and moved, so these methods simply send a message to a shape object; they don’t worry about how the object copes with the message. Feedback Such code is unaffected by the addition of new types, and adding new types is the most common way to extend an object-oriented program to handle new situations. For example, you can derive a new subtype of shape called pentagon without modifying the methods that deal only with generic shapes. This ability to easily extend a design by deriving new subtypes is one of the essential ways to encapsulate change. This greatly improves designs while reducing the cost of software maintenance. Feedback There’s a problem, however, with attempting to treat derived-type objects as their generic base types (circles as shapes, bicycles as vehicles, cormorants as birds, etc.). If a method is going to tell a generic shape to draw itself, or a generic vehicle to steer, or a generic bird to move, the compiler cannot know at compile time precisely what piece of code will be executed. That’s the whole point—when the message is sent, the programmer doesn’t want to know what piece of code will be executed; the draw method can be applied equally to a circle, a square, or a triangle, and the object will execute the proper code depending on its specific type. If you don’t have to know what piece of code will be executed, then when you add a new subtype, the code it executes can be different without requiring changes to the method call. Therefore, the compiler cannot know precisely what piece of code is executed, so what does it do? For example, in the following diagram the BirdController object just works with generic Bird objects and does not know what exact type they are. This is convenient from BirdController’s perspective because it doesn’t have to write special code to determine the exact type of Bird it’s working with or that Bird’s behavior. So how does it happen that, when move( ) is called while ignoring the specific type of Bird, the right behavior will occur (a Goose runs, flies, or swims, and a Penguin runs or swims)? Feedback
  • 2. The answer is the primary twist in object-oriented programming: the compiler cannot make a function call in the traditional sense. The function call generated by a non-OOP compiler causes what is called early binding, a term you may not have heard before because you’ve never thought about it any other way. It means the compiler generates a call to a specific function name and the linker resolves this call to the absolute address of the code to be executed. In OOP, the program cannot determine the address of the code until run time, so some other scheme is necessary when a message is sent to a generic object. Feedback To solve the problem, object-oriented languages use the concept of late binding. When you send a message to an object, the code being called isn’t determined until run time. The compiler does ensure that the method exists and performs type checking on the arguments and return value (a language in which this isn’t true is called weakly typed), but it doesn’t know the exact code to execute. Feedback To perform late binding, Java uses a special bit of code in lieu of the absolute call. This code calculates the address of the method body, using information stored in the object (this process is covered in great detail in Chapter 7). Thus, each object can behave differently according to the contents of that special bit of code. When you send a message to an object, the object actually does figure out what to do with that message. Feedback In some languages you must explicitly state that you want a method to have the flexibility of late-binding properties (C++ uses the virtual keyword to do this). In these languages, by default, methods are not dynamically bound. In Java, dynamic binding is the default behavior
  • 3. and you don’t need to remember to add any extra keywords in order to get polymorphism. Feedback Consider the shape example. The family of classes (all based on the same uniform interface) was diagrammed earlier in this chapter. To demonstrate polymorphism, we want to write a single piece of code that ignores the specific details of type and talks only to the base class. That code is decoupled from type-specific information and thus is simpler to write and easier to understand. And, if a new type—a Hexagon, for example—is added through inheritance, the code you write will work just as well for the new type of Shape as it did on the existing types. Thus, the program is extensible. Feedback If you write a method in Java (as you will soon learn how to do): Feedback void doStuff(Shape s) { s.erase(); // ... s.draw(); } This method speaks to any Shape, so it is independent of the specific type of object that it’s drawing and erasing. If some other part of the program uses the doStuff( ) method: Feedback Circle c = new Circle(); Triangle t = new Triangle(); Line l = new Line(); doStuff(c); doStuff(t); doStuff(l); the calls to doStuff( ) automatically work correctly, regardless of the exact type of the object. Feedback This is a rather amazing trick. Consider the line: doStuff(c);
  • 4. What’s happening here is that a Circle is being passed into a method that’s expecting a Shape. Since a Circle is a Shape it can be treated as one by doStuff( ). That is, any message that doStuff( ) can send to a Shape, a Circle can accept. So it is a completely safe and logical thing to do. Feedback We call this process of treating a derived type as though it were its base type upcasting. The name cast is used in the sense of casting into a mold and the up comes from the way the inheritance diagram is typically arranged, with the base type at the top and the derived classes fanning out downward. Thus, casting to a base type is moving up the inheritance diagram: “upcasting.” Feedback An object-oriented program contains some upcasting somewhere, because that’s how you decouple yourself from knowing about the exact type you’re working with. Look at the code in doStuff( ): Feedback s.erase(); // ... s.draw(); Notice that it doesn’t say “If you’re a Circle, do this, if you’re a Square, do that, etc.” If you write that kind of code, which checks for all the possible types that a Shape can actually be, it’s messy and you need to change it every time you add a new kind of Shape. Here, you just say “You’re a shape, I know you can erase( ) and draw( ) yourself, do it, and take care of the details correctly.” Feedback What’s impressive about the code in doStuff( ) is that, somehow, the right thing happens. Calling draw( ) for Circle causes different code to be executed than when calling draw( ) for a Square or a Line, but
  • 5. when the draw( ) message is sent to an anonymous Shape, the correct behavior occurs based on the actual type of the Shape. This is amazing because, as mentioned earlier, when the Java compiler is compiling the code for doStuff( ), it cannot know exactly what types it is dealing with. So ordinarily, you’d expect it to end up calling the version of erase( ) and draw( ) for the base class Shape, and not for the specific Circle, Square, or Line. And yet the right thing happens because of polymorphism. The compiler and run-time system handle the details; all you need to know right now is that it does happen, and more importantly, how to design with it. When you send a message to an object, the object will do the right thing, even when upcasting is involved. Feedback Abstract base classes and interfaces Often in a design, you want the base class to present only an interface for its derived classes. That is, you don’t want anyone to actually create an object of the base class, only to upcast to it so that its interface can be used. This is accomplished by making that class abstract by using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents it. This is a tool to enforce a particular design. Feedback You can also use the abstract keyword to describe a method that hasn’t been implemented yet—as a stub indicating “here is an interface method for all types inherited from this class, but at this point I don’t have any implementation for it.” An abstract method may be created only inside an abstract class. When the class is inherited, that method must be implemented, or the inheriting class becomes abstract as well. Creating an abstract method allows you to put a method in an interface without being forced to provide a possibly meaningless body of code for that method. Feedback The interface keyword takes the concept of an abstract class one step further by preventing any method definitions at all. The interface is a very handy and commonly used tool, as it provides the perfect separation of interface and implementation. In addition, you can combine many interfaces together, if you wish, whereas inheriting from multiple regular classes or abstract classes is not possible.