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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

More on Classes Inheritance and Polymorphism
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
January 31 Is-a Versus Has-a Polymorphism Abstract & Final Class/Method.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CS10092 : Inheritance in Java (#2 of 4) Gavin Brown
Programming Languages and Paradigms Object-Oriented Programming.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Polymorphism &Virtual Functions
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object-Oriented Programming with C++ Yingcai Xiao.
March 31, 2000CS102-01Lecture 1.3 Introduction to Object-Oriented Programming CS Lecture 1-3.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Information System Design (IT60105) Lecture 26 Object-Oriented System Testing.
Programming in Java CSCI-2220 Object Oriented Programming.
April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS Lecture 1-3.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Object Oriented Programming
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
April 27, 1998CS102-02Lecture 5-1 Object-Oriented Programming Revisited CS Lecture 5-1 The Wonderful World of Objects.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Overview of C++ Polymorphism
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Polymorphism.
Advanced Programming in Java
12 Data abstraction Packages and encapsulation
Object Oriented Analysis and Design
Overloading and Constructors
More inheritance, Abstract Classes and Interfaces
Virtual Functions Department of CSE, BUET Chapter 10.
Advanced Java Topics Chapter 9
Abstract Classes Page
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
Advanced Programming in Java
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

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

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

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

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

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

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

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": : }

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!

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!

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

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

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

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

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

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

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

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()

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

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?)