Polymorphism CSCI 201 Principles of Software Development

Similar documents
Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Inheritance, and Polymorphism.

Inheritance and Overloading. Week 11

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CMSC 132: Object-Oriented Programming II. Inheritance

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Exception Handling CSCI 201 Principles of Software Development

Friend Functions, Inheritance

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CSCI 201L Written Exam #1 Fall % of course grade

Extending Classes (contd.) (Chapter 15) Questions:

Monitors CSCI 201 Principles of Software Development

Concurrent Computing CSCI 201 Principles of Software Development

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Midterm Exam 5 April 20, 2015

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Fast Introduction to Object Oriented Programming and C++

Polymorphism Part 1 1

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Arrays Classes & Methods, Inheritance

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

CSCI 201L Written Exam #1 Fall % of course grade

Chapter 14 Abstract Classes and Interfaces

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

C18a: Abstract Class and Method

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

ITI Introduction to Computing II

Inheritance Motivation

ITI Introduction to Computing II

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Data Structures (INE2011)

Java Object Oriented Design. CSC207 Fall 2014

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

JAVA Programming Language Homework I - OO concept

CS-202 Introduction to Object Oriented Programming

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Final Exam. Name: Student ID: Section: Signature:

index.pdf January 21,

Programming in C# Inheritance and Polymorphism

Last class. -More on polymorphism -super -Introduction to interfaces

CS 162, Lecture 25: Exam II Review. 30 May 2018

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Object-Oriented Programming (OOP) Fundamental Principles of OOP

System Design and Programming II

ITI Introduction to Computing II

What is Inheritance?

Inheritance and Interfaces

12% of course grade. CSCI 201L Final - Written Fall /7

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

C++ Structures Programming Workshop 2 (CSCI 1061U)

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

ITI Introduction to Computing II

ECE 461 Fall 2011, Final Exam

Big software. code reuse: The practice of writing program code once and using it in many contexts.

OVERRIDING. 7/11/2015 Budditha Hettige 82

ECE 462 Exam 1. 09:30-10:20AM, September 24, 2008

Method Overriding in Java

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

AP Computer Science Unit 1. Programs

CSCI 201L Written Exam #1 - SOLUTION Fall % of course grade

Parallel Computing CSCI 201 Principles of Software Development

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 9 Inheritance

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Extending Classes (contd.) (Chapter 15) Questions:

C++ Programming Lecture 8 Software Engineering Group

Programming in the Large II: Objects and Classes (Part 2)

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

CSCI 201L Written Exam #1 Fall % of course grade

15: Polymorphism & Virtual Functions

VIRTUAL FUNCTIONS Chapter 10

CS2141 Software Development using C/C++ C++ Basics

Casting and polymorphism Enumeration

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Binghamton University. CS-140 Fall Dynamic Types

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Inheritance and Polymorphism

Advanced Placement Computer Science. Inheritance and Polymorphism

Programming by Delegation

Inheritance and Polymorphism

Chapter 1: Object-Oriented Programming Using C++

CSCI 201L Midterm Written SOLUTION Fall % of course grade

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Homework 6. Reading. Problems. Handout 7 CS242: Autumn November

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Transcription:

Polymorphism CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

Program Outline USC CSCI 201L

Polymorphism Based on the inheritance hierarchy, an object with a compile-time type of a parent can take on the form of a child at runtime Then, if the parent has a method declared in it, (note that this does not say defined or implemented) when that method is called on an object with a compile-time type of the parent, it will call the method in the run-time type of the object, which would be the child (this is Java-specific) You are only able to call methods based on the compile-time type of an object If a method only exists in the child and you have an instance of a parent, you won t be able to call the child method since not every child of that parent is required to implement that method You can downcast the parent object to a child, but this is potentially dangerous since you may not know the runtime type of the object USC CSCI 201L 3/18

Polymorphism Example 1 1 class Shape { 2 protected char name; 3 public Shape(char n) { 4 name = n; 5 } 6 public void printname() { 7 System.out.println(name); 8 } 9 } 10 class TwoD extends Shape { 11 public TwoD(char name) { 12 super(name); 13 } 14 } 15 class Square extends Rectangle { 16 public Square(char nm, float s) { 17 super(nm, s, s); 18 } 19 } 20 class Triangle extends TwoD { 21 private float base; 22 private float height; 23 public Triangle(char nm, float b, float h) { 24 super(nm); 25 base = b; 26 height = h; 27 } 28 public float getarea() { 29 return 0.5f * base * height; 30 } 31 } 32 class Rectangle extends TwoD { 33 protected float width, length; 34 public Rectangle(char nm, float w, float l) { 35 super(nm); 36 width = w; 37 length = l; 38 } 39 public float getarea() { 40 return width * length; 41 } 42 } USC CSCI 201L 4/18

Polymorphism Example 1 (cont.) 1 public class ShapeTest { 2 public static void printshape(shape s) { 3 System.out.print( First letter of shape is ); 4 s.printname(); 5 System.out.print( Shape area: + s.getarea()); 6 } 7 8 public static void main(string [] args) { 9 Shape sh; 10 if (args[0].equals( triangle )) { 11 sh = new Triangle( t, 5.0f, 4.0f); 12 } 13 else if (args[0].equals( rectangle )) { 14 sh = new Rectangle( r, 3.0f, 2.0f); 16 else { 17 sh = new Square( s, 4.0f); 18 } 19 printshape(sh); 20 } 21 } How would I print the area of the shape? USC CSCI 201L 5/18

Polymorphism Example 2 1 class Shape { 2 protected char name; 3 public Shape(char n) { 4 name = n; 5 } 6 public void printname() { 7 System.out.println(name); 8 } 9 public float getarea() { 10 return 0; // how to get area? 11 } 12 } 13 class TwoD extends Shape { 14 public TwoD(char name) { 15 super(name); 16 } 17 } 18 class Square extends Rectangle { 19 public Square(char nm, float s) { 20 super(nm, s, s); 21 } 22 } 23 class Triangle extends TwoD { 24 private float base; 25 private float height; 26 public Triangle(char nm, float b, float h) { 27 super(nm); 28 base = b; 29 height = h; 30 } 31 public float getarea() { 32 return 0.5f * base * height; 33 } 34 } 35 class Rectangle extends TwoD { 36 protected float width, length; 37 public Rectangle(char nm, float w, float l) { 38 super(nm); 39 width = w; 40 length = l; 41 } 42 public float getarea() { 43 return width * length; 44 } 45 } USC CSCI 201L 6/18

Polymorphism Example 2 (cont.) 1 public class ShapeTest { 2 public static void printshape(shape s) { 3 System.out.print( First letter of shape is ); 4 s.printname(); 5 System.out.print( Shape area: + s.getarea()); 6 } 7 8 public static void main(string [] args) { 9 Shape sh; 10 if (args[0].equals( triangle )) { 11 sh = new Triangle( t, 5.0f, 4.0f); 12 } 13 else if (args[0].equals( rectangle )) { 14 sh = new Rectangle( r, 3.0f, 2.0f); 16 else { 17 sh = new Square( s, 4.0f); 18 } 19 printshape(sh); 20 } 21 } Does this print the area of the shape? USC CSCI 201L 7/18

Polymorphism Example 3 1 abstract class Shape { 2 protected char name; 3 public Shape(char n) { 4 name = n; 5 } 6 public void printname() { 7 System.out.println(name); 8 } 9 public abstract float getarea(); 10 } 11 abstract class TwoD extends Shape { 12 public TwoD(char name) { 13 super(name); 14 } 16 class Square extends Rectangle { 17 public Square(char nm, float s) { 18 super(nm, s, s); 19 } 20 } 21 class Triangle extends TwoD { 22 private float base; 23 private float height; 24 public Triangle(char nm, float b, float h) { 25 super(nm); 26 base = b; 27 height = h; 28 } 29 public float getarea() { 30 return 0.5f * base * height; 31 } 32 } 33 class Rectangle extends TwoD { 34 protected float width, length; 35 public Rectangle(char nm, float w, float l) { 36 super(nm); 37 width = w; 38 length = l; 39 } 40 public float getarea() { 41 return width * length; 42 } 43 } USC CSCI 201L 8/18

Polymorphism Example 3 (cont.) 1 public class ShapeTest { 2 public static void printshape(shape s) { 3 System.out.print( First letter of shape is ); 4 s.printname(); 5 System.out.print( Shape area: + s.getarea()); 6 } 7 8 public static void main(string [] args) { 9 Shape sh; 10 if (args[0].equals( triangle )) { 11 sh = new Triangle( t, 5.0f, 4.0f); 12 } 13 else if (args[0].equals( rectangle )) { 14 sh = new Rectangle( r, 3.0f, 2.0f); 16 else { 17 sh = new Square( s, 4.0f); 18 } 19 printshape(sh); 20 } 21 } USC CSCI 201L 9/18

Advanced Polymorphism Example 1 class C0 extends C1 { 2 public int meth4() { 3 System.out.println("3"); 4 return 3; 5 } 6 } 7 8 class C1 extends C2 implements I1, I2 { 9 public void meth1(int num) { 10 System.out.println(num); 11 } 12 public int meth2() { 13 System.out.println("1"); 14 return 1; 16 public int meth3() { 17 System.out.println("2"); 18 return 2; 19 } 20 } 21 22 abstract class C2 extends C3 { 23 public void foo() { 24 System.out.println("foo"); 25 } 26 } 27 28 abstract class C3 { 29 public abstract void meth1(int i); 30 } 31 32 interface I1 { 33 public int meth2(); 34 } 35 interface I2 { 36 public int meth3(); 37 } 38 39 40 public class Test { 41 public static void main(string [] args) { 42 C1 c = new C0(); 43 c.meth1(0); 44 c.meth2(); 45 c.meth3(); 46 c.meth4(); 47 c.foo(); 48 } 49 } USC CSCI 201L 10/18

Advanced Polymorphism Fix #1 1 class C0 extends C1 { 2 public int meth4() { 3 System.out.println("3"); 4 return 3; 5 } 6 } 7 8 class C1 extends C2 implements I1, I2 { 9 public void meth1(int num) { 10 System.out.println(num); 11 } 12 public int meth2() { 13 System.out.println("1"); 14 return 1; 16 public int meth3() { 17 System.out.println("2"); 18 return 2; 19 } 20 } 21 22 abstract class C2 extends C3 { 23 public void foo() { 24 System.out.println("foo"); 25 } 26 } 27 28 abstract class C3 { 29 public abstract void meth1(int i); 30 } 31 32 interface I1 { 33 public int meth2(); 34 } 35 interface I2 { 36 public int meth3(); 37 } 38 39 40 public class Test { 41 public static void main(string [] args) { 42 C1 c = new C0(); 43 c.meth1(0); 44 c.meth2(); 45 c.meth3(); 46 // c.meth4(); 47 c.foo(); 48 } 49 } USC CSCI 201L 11/18

Advanced Polymorphism Fix #2 1 class C0 extends C1 { 2 public int meth4() { 3 System.out.println("3"); 4 return 3; 5 } 6 } 7 8 class C1 extends C2 implements I1, I2 { 9 public void meth1(int num) { 10 System.out.println(num); 11 } 12 public int meth2() { 13 System.out.println("1"); 14 return 1; 16 public int meth3() { 17 System.out.println("2"); 18 return 2; 19 } 20 } 21 22 abstract class C2 extends C3 { 23 public void foo() { 24 System.out.println("foo"); 25 } 26 } 27 28 abstract class C3 { 29 public abstract void meth1(int i); 30 } 31 32 interface I1 { 33 public int meth2(); 34 } 35 interface I2 { 36 public int meth3(); 37 } 38 39 40 public class Test { 41 public static void main(string [] args) { 42 C0 c = new C0(); 43 c.meth1(0); 44 c.meth2(); 45 c.meth3(); 46 c.meth4(); 47 c.foo(); 48 } 49 } USC CSCI 201L 12/18

Advanced Polymorphism Fix #3 1 class C0 extends C1 { 2 public int meth4() { 3 System.out.println("3"); 4 return 3; 5 } 6 } 7 8 abstract class C1 extends C2 implements I1, I2 { 9 public void meth1(int num) { 10 System.out.println(num); 11 } 12 public int meth2() { 13 System.out.println("1"); 14 return 1; 16 public int meth3() { 17 System.out.println("2"); 18 return 2; 19 } 20 public abstract int meth4(); 21 } 22 23 abstract class C2 extends C3 { 24 public void foo() { 25 System.out.println("foo"); 26 } 27 } 28 28 abstract class C3 { 29 public abstract void meth1(int i); 30 } 31 32 interface I1 { 33 public int meth2(); 34 } 35 interface I2 { 36 public int meth3(); 37 } 38 39 40 public class Test { 41 public static void main(string [] args) { 42 C1 c = new C0(); 43 c.meth1(0); 44 c.meth2(); 45 c.meth3(); 46 c.meth4(); 47 c.foo(); 48 } 49 } USC CSCI 201L 13/18

Advanced Polymorphism Fix #4 1 class C0 extends C1 { 2 public int meth4() { 3 System.out.println("3"); 4 return 3; 5 } 6 } 7 8 abstract class C1 extends C2 implements I1, I2 { 9 public void meth1(int num) { 10 System.out.println(num); 11 } 12 public int meth2() { 13 System.out.println("1"); 14 return 1; 16 public int meth3() { 17 System.out.println("2"); 18 return 2; 19 } 20 } 21 22 abstract class C2 extends C3 { 23 public void foo() { 24 System.out.println("foo"); 25 } 26 } 27 28 abstract class C3 { 29 public abstract void meth1(int i); 30 } 31 32 interface I1 { 33 public int meth2(); 34 } 35 interface I2 extends I3 { 36 public int meth3(); 37 } 38 39 interface I3 { // is I3 necessary? 40 public int meth4(); 41 } 42 43 public class Test { 44 public static void main(string [] args) { 45 C1 c = new C0(); 46 c.meth1(0); 47 c.meth2(); 48 c.meth3(); 49 c.meth4(); 50 c.foo(); 51 } 52 } USC CSCI 201L 14/18

Redefining/Overriding A derived class in C++ is able to redefine a method in a base class by giving the method the same name and parameters Java and C++ behave differently C++ If the function in the parent is virtual, the function in the child overrides it. Then the function executed is based on the runtime type of the variable. If the function in the parent is not virtual, the function in the child redefines it. Then the function executed is based on the compile-time type of the variable. Java There is no redefining of methods in Java. Methods that are defined in a derived class with the same signature as they are defined in a base class are always overridden When the method is called on an instance of either the derived class or the base class, the method in the runtime instance of the object is called Java doesn t have a sense of redefining methods - it always overrides, regardless of the use of the word abstract USC CSCI 201L 15/18

Redefining Methods/Functions Example NOTE: Java does not have redefined methods These methods have all been overridden NOTE: C++ functions are redefined if they are not defined as virtual in the parent 1 class C0 { 2 public void meth3() { 3 System.out.println("3"); 4 } 5 } 6 7 class C1 extends C0 { 8 public void meth3() { 9 System.out.println("4"); 10 } 11 } 12 public class HelloClass { 13 public static void meth(c0 c) { 14 c.meth3(); 16 public static void main(string [] args) { 17 C0 c0 = new C1(); 18 c0.meth3(); 19 20 meth(c0); 21 22 C1 c1 = new C1(); 23 c1.meth3(); 24 meth(c1); 25 } 26 } 1 #include <iostream> 2 using namespace std; 3 class C0 { 4 public: 5 void meth3() { 6 cout << "3" << endl; 7 } 8 }; 9 class C1 : public C0 { 10 public: 11 void meth3() { 12 cout << "4" << endl; 13 } 14 }; 15 void meth(c0 *c) { 16 c->meth3(); 17 } 18 void main() { 19 C0 *c0 = new C1(); 20 c0->meth3(); 21 22 meth(c0); 23 24 C1 *c1 = new C1(); 25 c1->meth3(); 26 meth(c1) 27 } USC CSCI 201L 16/18

Program Outline USC CSCI 201L

Program Write the shape program provided in these slides, but make the Shape class an interface instead. Make the name variable protected so it can be set in the derived classes. Think about the advantages to using an interface instead of a class. c:\>java csci201.shapetest triangle First letter of shape is t Shape area: 10.0 c:\> Program USC CSCI 201L 18/18