Presentation is loading. Please wait.

Presentation is loading. Please wait.

April 29, 1998CS102-02Lecture 5-2 Object-Oriented Programming: Polymorphism & Abstraction CS 102-02 Lecture 5-2 The Wonderful World of Objects.

Similar presentations


Presentation on theme: "April 29, 1998CS102-02Lecture 5-2 Object-Oriented Programming: Polymorphism & Abstraction CS 102-02 Lecture 5-2 The Wonderful World of Objects."— Presentation transcript:

1 April 29, 1998CS102-02Lecture 5-2 Object-Oriented Programming: Polymorphism & Abstraction CS 102-02 Lecture 5-2 The Wonderful World of Objects

2 April 29, 1998CS102-02Lecture 5-2 A Brief Outline of Today’s Events The Many Shapes of Polymorphism Dynamic binding Cutting off inheritance Abstract classes

3 April 29, 1998CS102-02Lecture 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

4 April 29, 1998CS102-02Lecture 5-2 Similarities Abound

5 April 29, 1998CS102-02Lecture 5-2 Two Cars Similarities –Internal-combustion engine –Automobiles –Steering wheel –Accelerator pedal Differences –Transmission (auto vs. manual) –Six-speed gearbox (F50) –Engine placement –Seating

6 April 29, 1998CS102-02Lecture 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

7 April 29, 1998CS102-02Lecture 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": : }

8 April 29, 1998CS102-02Lecture 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!

9 April 29, 1998CS102-02Lecture 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!

10 April 29, 1998CS102-02Lecture 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

11 April 29, 1998CS102-02Lecture 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

12 April 29, 1998CS102-02Lecture 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

13 April 29, 1998CS102-02Lecture 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

14 April 29, 1998CS102-02Lecture 5-2 Abstract Classes Inheritance enables us to extract the commonalities among objects

15 April 29, 1998CS102-02Lecture 5-2 Incomplete Specification "Build a plane!" –Powerplant: jet, turboprop or normally- aspirated? –Pressurized? –Retractable gear? –High-wing or low-wing? The notion of plane is still useful though –Captures what's common all planes

16 April 29, 1998CS102-02Lecture 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

17 April 29, 1998CS102-02Lecture 5-2 An Abstract Class Point abstract class Point { int x = 1, y = 1; void move(int dx, int dy) { x += dx; y += dy; alert(); } abstract void alert(); } Can't create Point objects No implementation for alert()

18 April 29, 1998CS102-02Lecture 5-2 Getting More Specific abstract class ColoredPoint extends Point { int color; } SimplePoint is not abstract: class SimplePoint extends Point { void alert() { } } ColoredPoint MUST be abstract

19 April 29, 1998CS102-02Lecture 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?)


Download ppt "April 29, 1998CS102-02Lecture 5-2 Object-Oriented Programming: Polymorphism & Abstraction CS 102-02 Lecture 5-2 The Wonderful World of Objects."

Similar presentations


Ads by Google