4-2 OOP not done in class

Download Report

Transcript 4-2 OOP not done in class

Object-Oriented Programming:
Polymorphism & Abstraction
The Wonderful World of Objects
CS 102-02
Lecture 5-2
April 29, 1998
CS102-02
Lecture 5-2
A Brief Outline of Today’s
Events
•
•
•
•
The Many Shapes of Polymorphism
Dynamic binding
Cutting off inheritance
Abstract classes
April 29, 1998
CS102-02
Lecture 5-2
Polymorphism
• Polymorphism is all about adjusting to
the subtle nuances of the world, and
doing so with grace and ease.
• Objects often have similar functionality,
but implemented differently
April 29, 1998
CS102-02
Lecture 5-2
Similarities Abound
April 29, 1998
CS102-02
Lecture 5-2
Two Cars
• Similarities
• Differences
– Internal-combustion
engine
– Automobiles
– Steering wheel
– Accelerator pedal
April 29, 1998
CS102-02
– Transmission (auto
vs. manual)
– Six-speed gearbox
(F50)
– Engine placement
– Seating
Lecture 5-2
The Problem
• What happens when you want to shift
gears in a car?
• Objects know how to do things
– Malibus know how to shift gears
– F50's know how to shift gears
April 29, 1998
CS102-02
Lecture 5-2
Switching on Type
• Should we call the F50's shift()
method or the Malibu's shift()
method?
• Each class includes a type member
– F50 class has a carType value set to
"F50"
switch (car.carType) {
case "F50":
:
}
April 29, 1998
CS102-02
Lecture 5-2
Polymorphism to the Rescue
You shouldn't have to know whether the
car is an F50 or a Malibu…
with polymorphism, you don't!
April 29, 1998
CS102-02
Lecture 5-2
Using Polymorphism
• Suppose that: F50 and Malibu are
both subclasses of the Car class
• Next, treat both F50's and Malibu's as
Car objects
• Finally, write:
auto.shift(gearThree)
Don't care whether it's an F50 or a Chevy!
April 29, 1998
CS102-02
Lecture 5-2
The Price of Polymorphism
• Polymorphism is harder on Java
– Java has to wait until the program runs,
and then decide which method to call
– More work has to be done when the
program runs
• Deciding which method goes with which
object is called binding
– Checking this at run-time is called run-time
or dynamic binding
April 29, 1998
CS102-02
Lecture 5-2
Java vs. C++
• C++ allows dynamic binding as well, but
you have to ask for it
– In C++, only virtual functions are bound at
run-time because:
– Dynamic binding slows things down, and
– Makes objects a little bigger
• Java sacrifices some performance for
ease of use
April 29, 1998
CS102-02
Lecture 5-2
Disinheriting Your Subclasses
• If you use private, subclasses can't
access data and call methods
• What if you want subclasses to access
your methods, but not create their own?
– Use final to say: creating subclasses is
okay, but you can't override this method's
implementation
April 29, 1998
CS102-02
Lecture 5-2
Cutting Off Inheritance
• What if you don't want anyone create a
subclass from your class?
– Make the whole class final
• For classes, final says:
– You can't extend this class to create
subclass
• java.lang.Math is declared as:
public final class Math
April 29, 1998
CS102-02
Lecture 5-2
Abstract Classes
• Inheritance enables us to extract the
commonalities among objects
Vehicles
Vehicle
Plane
Train
Automobile
Car
April 29, 1998
CS102-02
Lecture 5-2
Truck
Incomplete Specification
• "Build a plane!"
– Powerplant: jet, turboprop or normallyaspirated?
– Pressurized?
– Retractable gear?
– High-wing or low-wing?
• The notion of plane is still useful though
– Captures what's common all planes
April 29, 1998
CS102-02
Lecture 5-2
Abstract Classes
• Use abstract to say:
– This class is a useful grouping of common
features, but you'll have to be more specific
to create an actual object
• Can't create an object from an
abstract class
April 29, 1998
CS102-02
Lecture 5-2
An Abstract Class Point
Can't create
Point objects
abstract class Point {
int x = 1, y = 1;
void move(int dx, int dy) {
x += dx;
y += dy;
No implementation
alert();
for alert()
}
abstract void alert();
}
April 29, 1998
CS102-02
Lecture 5-2
Getting More Specific
ColoredPoint MUST
be abstract
abstract class ColoredPoint extends Point {
int color;
}
SimplePoint is not abstract:
class SimplePoint extends Point {
void alert() { }
}
April 29, 1998
CS102-02
Lecture 5-2
Use of Abstract Classes
• Abstract classes
– Group common attributes together, even if
there aren't enough common attributes to
completely specify an object
• Abstract methods
– Enforce a base level of functionality in
objects (how?)
April 29, 1998
CS102-02
Lecture 5-2