[ L5P1] Object-Oriented Programming: Advanced Concepts

Similar documents
INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Practice for Chapter 11

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance and object compatibility

Chapter 5 Object-Oriented Programming

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming: Based on slides from Skrien Chapter 2

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS111: PROGRAMMING LANGUAGE II

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Inheritance, Polymorphism, and Interfaces

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 5. Inheritance

First IS-A Relationship: Inheritance

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Chapter 14 Abstract Classes and Interfaces

Inheritance -- Introduction

CS111: PROGRAMMING LANGUAGE II

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Programming II (CS300)

What are the characteristics of Object Oriented programming language?

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

More Relationships Between Classes

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

COP 3330 Final Exam Review

OVERRIDING. 7/11/2015 Budditha Hettige 82

Java How to Program, 8/e

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS 251 Intermediate Programming Inheritance

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

CSCE3193: Programming Paradigms

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CST242 Object-Oriented Programming Page 1

Lecture Notes on Programming Languages

Inheritance and Polymorphism. CS180 Fall 2007

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

The major elements of the object-oriented model

Introduction to Object-Oriented Programming

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

PROGRAMMING LANGUAGE 2

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman


Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

Type Hierarchy. Lecture 6: OOP, autumn 2003

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Polymorphism and Inheritance

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

CMSC 132: Object-Oriented Programming II

More on Inheritance. Interfaces & Abstract Classes

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Polymorphism. Arizona State University 1

What is Inheritance?

VIRTUAL FUNCTIONS Chapter 10

C++ Programming: Polymorphism

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Lecture 18 CSE11 Fall 2013 Inheritance

Object-Oriented Design

Data Abstraction. Hwansoo Han

Introduction to Inheritance

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

CS112 Lecture: Inheritance and Polymorphism

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

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

Inheritance. Transitivity

CSE 307: Principles of Programming Languages

BBM 102 Introduction to Programming II Spring Inheritance

CSC207 Week 3. Larry Zhang

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

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

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

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS-202 Introduction to Object Oriented Programming

Questions Answer Key Questions Answer Key Questions Answer Key

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Object Oriented Issues in VDM++

Inheritance (continued) Inheritance

Conformance. Object-Oriented Programming Spring 2015

Data Structures and Other Objects Using C++

Comp 249 Programming Methodology

301AA - Advanced Programming [AP-2017]

C++ Important Questions with Answers

8. Polymorphism and Inheritance

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Inheritance and Interfaces

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Programming Exercise 14: Inheritance and Polymorphism

Inheritance and Polymorphism

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Chapter 2: Java OO II X I A N G Z H A N G

Transcription:

[ L5P1] Object-Oriented Programming: Advanced Concepts Polymorphism Polymorphism is an important and useful concept in the object-oriented paradigm. Take the example of writing a payroll application for a university to facilitate payroll processing of university staff. Suppose an adjustsalary(int) operation adjusts the salaries of all staff members. This operation will be executed whenever the university initiates a adjustment for its staff. However, the adjustment formula is different for different staff categories, say admin and academic. Here is one possible way of designing the classes in the Payroll system. Payroll adjustsalary(int) * * dostuff() Here is the implementation of the adjustsalary(int) operation from the above design. Similar processing. Note how processing is similar for the two staff types. It is as if the type of staff members is irrelevant to they are processed inside this operation! If that is the case, can the staff type be abstracted away from this operation? After all, why keep irrelevant details? Here is such an implementation of adjustsalary(int): 1 2

Only one data structure. Contains both and objects but treats them as objects Only one loop. Outcome of this call varies based on whether s is an or. The above code is better in several ways: It is shorter. It simpler. It is more flexible (this code will remain the same even if more staff types are added). This does not imply getting rid of the and classes completely and replacing them with a more general class called. Rather, this part of the code treats both and objects as one type called. For example, ArrayList staff contains both and objects although it is treated as an ArrayList of objects. However, when the operation of these objects is called, the resulting adjustment will be different for objects and objects. Therefore, different types of objects are treated as a single general type, but yet each type of object exhibits a different kind of behavior. This is called polymorphism (literally, it means ability to take many forms ). In this example, an object that is perceived as type can be an object or an object. To retain the multiple types while still allowing some parts of the system to treat them as one type, inheritance (class inheritance or interfaces inheritance) can be used. Here is a possible design that uses class inheritance. arrangemeeting() Given below is the minimum code for,, and classes. class { String ; double ; void adjustmysalary(int percent) { // do nothing //------------------------------------------ 3

class extends { @Override void adjustmysalary(int percent) { = * percent; //------------------------------------------ class extends { @Override void adjustmysalary(int percent) { = * percent * 2; Using the above example, there are three issues that are at the center of how polymorphism is achieved: substitutability, operation overriding, and dynamic binding. Operation overriding To get polymorphic behavior from an operation, the operation in the superclass needs to be redefined in each of the subclasses. This is called overriding. i.e. and override the of. Note that Payroll cannot use the operation unless it is present in the class. That is because adjustsalary(int) of Payroll is treating all objects in the ArrayList as objects. Therefore, it can use only those operations available in the class. How does overriding differ from overloading? The difference is related to the type signature of operations. The type signature of an operation is the type sequence of the parameters. The return type and parameter s are not part of the type signature. However, the parameter order is significant. Here are some examples: Method int Add(int X, int Y) void Add(int A, int B) void m(int X, double Y) Type Signature (int, int) (int, int) (int, double) void m(double X, int X) (double, int) Operation overloading arises when there are multiple operations with the same but different type signatures. Given below are two examples. 4

class Account { Account () { // Signature: ( )... Account (String, String number, double balance) { // Signature: (String,String,double)...... The constructor has been overloaded void calculatecap (String matric) {... void calculatecap (int[] averages) {... Overloading is used to indicate that multiple operations do similar things but take different parameters. An operation can be overloaded inside the same class or in sub/super classes. Overloading is resolved at compile time: i.e. the compiler uses operation and type signature to determine which operation to invoke. a=new Account() //invokes the 1st constructor b=new Account( a, b,2.0) //invokes the 2nd constructor In contrast, overriding arises when a sub-class redefines an operation using the same method and the same type signature. E.g. of is overriding the operation of. Dynamic binding Overridden operations are resolved at runtime. That is, the runtime decides which version of the operation should be executed based on the actual type of the receiving object. This is also called dynamic binding (sometimes called late binding 3 and run-time binding). Most OO languages support dynamic binding. Note: The opposite of dynamic binding is static binding (also called early binding and compile-time binding. As explained in the previous section, operation overloading is handled using static binding. Consider the example code given below. The declared type of s is and it appears as if the operation of the class is invoked. However, at runtime the operation of the actual object will be called (i.e. operation of or ). If the actual object does not override that operation, the operation defined in the immediate superclass (in this case, class) will be called. void adjustsalary(int bypercent) { for( s: staff) { s.adjustmysalary(bypercent); Abstract classes/operations Since does not need a full implementation in the class, only its operation header needs to be defined in the class without specifying its body. Such a declaration without the operation body is called an abstract operation. In this context, abstract means not fully specified. A class that has at least one abstract operation becomes an abstract class itself. i.e. no object instances can be created from it. This is logical because it does not make sense to 3 There are subtle differences between late binding and dynamic binding, but they are beyond the scope of this handout. 5

create objects which are neither objects nor objects. In any case, instances of an abstract class cannot be created because the definition of the class is now incomplete. For example, line 1 below is not allowed but line 2 is allowed. s1 = new (); // line 1, not allowed s2 = new (); //line 2, allowed Furthermore, some OOP languages allow declaring a class as abstract even if it does not have any abstract operations so as to prevent it from being instantiated. Note: The term concrete class is used to distinguish a normal class from an abstract class. i.e. if a class is not an abstract class, then it is a concrete class. A sub class should implement all abstract methods of all its super classes, or otherwise declare itself as abstract. In UML, italics or the {abstract label are used to indicate the abstract operations/classes. Just a declaration; no method body. A non-abstract sub class should implement all abstract methods of the super class(es). OR {abstract {abstract Multiple inheritance In the example below, the TA class inherits from as well as Student. Such multiple inheritance is allowed among C++ classes but not among Java classes. Student takecourse() TA arrangemeeting() 6

Java allows multiple inheritance among interfaces. It also allows a class to implement multiple interfaces. The design given below is allowed in Java. <<interface>> Student <<interface>> <<interface>> TaxPayer <<interface>> Citizen TA Class vs Abstract Class vs Interface An interface is a behavior specification with no implementation. A class is a behavior specification + implementation. An abstract class is a behavior specification + partial implementation. Liskov Substitution Principle (LSP) Liskov Substitution Principle states that if a program module is using a super class, then the reference to the super class can be replaced with a sub class without affecting the functionality of the program module. As we know, Java already allows substituting objects of sub classes where an object of the super class is expected. However, that does not mean that doing so will not break the functionality of the code. To follow LSP, we should be careful not to contradict the behavior specified by the super class. Payroll arrangemeeting() For example, let us assume the Payroll class depends on the adjustmysalary(int percent) method of the class. Furthermore, the class states that the adjustmysalary method will work for all positive percent values. Both and classes override the adjustmysalary method. Now consider the following: ::adjustmysalary method works for both negative and positive percent values. ::adjustmysalary method works for percent values 1..100 only. 7

In the above scenario, class follows LSP because it fulfills Payroll s expectation of objects (i.e. it works for all positive values) but the class violates LSP because it will not work for percent values over 100 as expected by the Payroll class. That is, substituting objects for objects will not break the Payroll class, but substituting objects for objects will break the Payroll functionality. Worked examples [Q1] Jim recently learned the following four things in a programming class. a. Inheritance b. Dynamic binding c. Overriding d. Substitutability However, Jim has no idea what polymorphism is. You job is to prepare a short write-up (about 1 page) to explain what polymorphism is and how a d above work together to achieve polymorphic behavior. You may use diagrams of any sort, code snippets, and pseudo-code in your answer. You may also use the Payroll example from the handout. Note that Jim already knows what each of a-d means individually. We do not have to explain each in-detail again. What he does not know is what polymorphism is and how a-d combines to achieve polymorphism. [A2] Polymorphism is the ability to treat multiple types as a single general type and still get different behavior from each of those types. For example, the loop at line 5 in the below code treats and objects as a single type called and still the result of the adjustmysalary differs based on whether s is an object or an object. ArrayList<> staff = new ArrayList<>(); //line 1 staff.add(new ( Sam )); //line 2 staff.add(new ( Dilbert )); //line 3 staff.add(new ( Mui Kiat )); //line 4 for ( s: staff) { //line 5 s.adjustmysalary(); According to line 1, staff ArrayList expects objects. Yet, we can add and objects to it (lines 2-4). That is an example of the ability to treat multiple types as a single general type. This ability is a result of substitutability, which states that wherever an object of super type is expected, we should be able to substitute a subtype. This implies that and are sub types of. One way we can achieve this is using inheritance. That is, we can make and subclasses of. Overriding (2) 8

The ability to still get different behavior from each of those types is given by dynamic binding. That is, the runtime dynamically checks the actual type of the object (irrespective of its declared type) and binds to the most concrete definition of an operation (i.e. the one that is defined in the lowest level of the inheritance hierarchy). That means, if we override the adjustmysalary operation in and objects, those operations will be the ones invoked during runtime instead of the one defined in the class. This way, we get the behavior of and objects although the declared type of s is. [There you have it Jim; that is how Inheritance, Dynamic binding, Overriding, and Substitutability combine to achieve polymorphism.] [Q2] Given below is a partial class diagram from the Minesweeper game. Explain at least one instance where you can take advantage of polymorphism when implementing this class diagram. State which classes and which operations will be involved and what is the polymorphic behavior you expect. Minefield W*H Cell MinedCell 1 Mine MineFreeCell digit: ADJACENT_MINES [A2] The mark operation in Cell class can be abstract and can be overridden in MinedCell and MineFreeCell classes. The Minefield object can treat all cells as of type Cell. When a cell is marked by the player, Minefield object can simply call the abstract mark() operation of the Cell class and still get a different behavior depending on whether the actual cell object is a MinedCell or a MineFreeCell. The same can be applied to clear() operation. {abstract Cell +mark() {abstract MinedCell +mark() MineFreeCell digit: ADJACENT_MINES +mark() -- End of handout -- 9