ITI Introduction to Computing II

Similar documents
ITI Introduction to Computing II

index.pdf January 21,

ITI Introduction to Computing II

ITI Introduction to Computing II

ITI Introduction to Computing II

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

Java Object Oriented Design. CSC207 Fall 2014

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Introduction to Programming Using Java (98-388)

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Making New instances of Classes

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

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

Inheritance and Polymorphism

Use the scantron sheet to enter the answer to questions (pages 1-6)

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

VARIABLES AND TYPES CITS1001

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Inheritance, Polymorphism, and Interfaces

Practice for Chapter 11

CLASS DESIGN. Objectives MODULE 4

CS-202 Introduction to Object Oriented Programming

CS 251 Intermediate Programming Inheritance

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

JAVA MOCK TEST JAVA MOCK TEST II

VIRTUAL FUNCTIONS Chapter 10

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

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

Inheritance Motivation

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

CSE 143 Lecture 20. Circle

Object-Oriented Programming More Inheritance

Chapter 14 Abstract Classes and Interfaces

Introduction to Inheritance

Advanced Placement Computer Science. Inheritance and Polymorphism

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Example: Count of Points

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Inheritance, polymorphism, interfaces

OBJECT ORİENTATİON ENCAPSULATİON

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

COURSE 2 DESIGN PATTERNS

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

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

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

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

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance, and Polymorphism.

INHERITANCE. Spring 2019

Inheritance and Polymorphism

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

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

CS111: PROGRAMMING LANGUAGE II

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

Instance Members and Static Members

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Inheritance (continued) Inheritance

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

CS1150 Principles of Computer Science Objects and Classes

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

Lecture 5: Inheritance

Introduction to Object-Oriented Programming

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

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

First IS-A Relationship: Inheritance

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

CS260 Intro to Java & Android 03.Java Language Basics

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

G Programming Languages - Fall 2012

More Relationships Between Classes

CS 11 java track: lecture 3

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

PROGRAMMING LANGUAGE 2

C18a: Abstract Class and Method

Polymorphism CSCI 201 Principles of Software Development

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

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

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Polymorphism and Interfaces. CGS 3416 Spring 2018

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

8. Polymorphism and Inheritance

ITI Introduction to Computing II

C++ (classes) Hwansoo Han

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

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Transcription:

ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.

Circle Let s complete the implementation of the class Circle. Where would you implement the method area()? In the class Shape or int the class Circle?

Circle public class Circle extends Shape { private double radius; public double getradius() { return radius; public double area() { return Math.PI * radius * radius; public void scale( double factor ) { radius *= factor;

Rectangle Similarly, let s complete the implementation of the class Rectangle. Where would you implement the method area()? In the class Shape or int the class Rectangle?

Rectangle public class Rectangle extends Shape { private double width; private double height; //... public double area() { return width * height; public void scale(double factor) { width = width * factor; height = height * factor;

Don t get the wrong impression that inheritance is restricted to the classes that you are defining yourself. Inheritance is often used to specialize existing classes of the Java library. import java.awt.textfield; public class TimeField extends TextField { public Time gettime() { return Time.parseTime( gettext() ); // java.lang.object // // +--java.awt.component // // +--java.awt.textcomponent // // +--java.awt.textfield // // +--TimeField

Polymorphism From the Greek words polus = many and morphê = forms, literally means has many forms. 1. Ad hoc polymorphism (overloading): a method name is associcated with different blocs of code 2. Inclusion (subtyping, data) polymorphism: an identifer (a reference variable) is associated with data of different types with the use of a subtype relation In Java, a variable or a method is polymorphic if it refers to objects of more than one class/type.

Method overloading Method overloading means that two methods can have the same name but different signatures (the signature consists of the name and formal parameters of a method but not the return value). Constructors are often overloaded, this occurs for the class Shape: Shape() { x = 0.0; y = 0.0; Shape( int x, int y ) { this.x = x; this.y = y; Method overloading is sometimes referred to as ad hoc polymorphism (ad hoc = for a specific purpose).

Overloading (contd) In Java, some operators are overloaded, consider the + which adds two numbers or concatenates two strings, a user can overload a method but not an operator. Since the signatures are different, Java has no problem finding the right method: static int sum( int a, int b, int c ) { return a + b + c; static int sum( int a, int b ) { return a + b; static double sum( double a, double b ) { return a + b;

Overloading (contd) The class PrintStream has a specific println method for each primitive type (a good example of overloading): println() println( boolean ) println( char ) println( char[] ) println( double ) println( float ) println( int ) println( long )

Overloading (contd) Pros: all the methods that implement a similar behaviour have the same name. Cons: still have to provide one implementation for each behaviour.

True polymorphism: motivation 1 Problem: implement the method isleftof that returns true if this Shape is to the left of its argument.

Circle c1, c2; c1 = new Circle( 10, 20, 5 ); c2 = new Circle( 20, 10, 5 ); isleftof if ( c1.isleftof( c2 ) ) { System.out.println( "c1 isleftof c2" ); else { System.out.println( "c2 isleftof c1" );

isleftof Rectangle r1, r2; r1 = new Rectangle( 0, 0, 1, 1 ); r2 = new Rectangle( 100, 100, 200, 400 ); if ( r1.isleftof( r2 ) ) { System.out.println( "r1 isleftof r2" ); else { System.out.println( "r2 isleftof r1" );

isleftof if ( r1.isleftof( c1 ) ) { System.out.println( "r1 isleftof c1" ); else { System.out.println( "c1 isleftof r1" ); if ( c2.isleftof( r2 ) ) { System.out.println( "c2 isleftof r2" ); else { System.out.println( "r2 isleftof c2" );

Absurd solution! public boolean isleftof( Circle c ) { return getx() < c.getx(); public boolean isleftof( Rectangle r ) { return getx() < r.getx(); Why is that solution absurd?

Absurd solution! public boolean isleftof( Circle c ) { return getx() < c.getx(); public boolean isleftof( Rectangle r ) { return getx() < r.getx(); As many implementations as kinds of shape! All the implementations are the same! Whenever a new kind of Shape is defined (say Triangle) then a method ileftof must be created!

Solution What do you propose? The method getx() is common to all the Shapes; all shapes have a getx(). public boolean isleftof( Any Shape s ) { return getx() < s.getx(); How does one write Any Shape?

Solution Implement the method isleftof in the class Shape as follows. public boolean isleftof( Shape s ) { return getx() < s.getx();

Circle c; c = new Circle( 10, 20, 5 ); isleftof Rectangle r; r = new Rectangle( 0, 0, 1, 1 ); if ( c.isleftof( r ) ) { System.out.println( "c isleftof r" ); else { System.out.println( "r isleftof c" );

isleftof if ( c.isleftof( r ) ) { //... The method isleftof of the object designated by c is called. Okay, c designates an object of the class Circle, which inherits the method isleftof.

isleftof if ( c.isleftof( r ) ) { //... Hum, when the method isleftof is called, the value of the actual parameter, r, is copied into the formal parameter s. Does it mean that the following statements are valid!? Shape s; Rectangle r; r = new Rectangle( 0, 0, 1, 1 ); s = r;

Types A variable is a storage location and has an associated type, sometimes called its compile-time type, that is either a primitive type ( 4.2) or a reference type ( 4.3). A variable always contains a value that is assignment compatible ( 5.2) with its type. Assignment of a value of compile-time reference type S (source) to a variable of compile-time reference type T (target) is checked as follows: If S is a class type: If T is a class type, then S must either be the same class as T, or S must be a subclass of T, or a compile-time error occurs. Gosling et al. (2000) The Java Language Specification.

isleftof Based on that definition, the following statements are valid. Shape s; Rectangle r; r = new Rectangle( 0, 0, 1, 1 ); s = r; but r = s is not!

Polymorphic variable The variable s designates any object that is from a subclass of Shape. Shape s; Usage: s = new Circle( 0, 0, 1 ); s = new Rectangle( 10, 100, 10, 100 );

Polymorphic method: true polymorphism public boolean isleftof( Shape other ) { boolean result; if ( getx() < other.getx() ) { result = true; else { result = false; return result; Usage: Circle c = new Circle( 10, 10, 5 ); Rectangle d = new Rectangle( 0, 10, 12, 24 ); if ( c.isleftof( d ) ) {...

Shape s; Circle c; c = new Circle( 0, 0, 1 ); s = c; Polymorphic variable (contd) if ( c.getx() ) {... // valid? if ( s.getx() ) {... // valid? if ( c.getradius() ) {... // valid? if ( s.getradius() ) {... // valid?

Shape s; Circle c; c = new Circle( 0, 0, 1 ); s = c; Polymorphic variable (contd) The object designated by s is still a Circle. The class of object does not change during the execution of the program.

Shape s; Circle c; c = new Circle( 0, 0, 1 ); s = c; if ( s.getx() ) {... Polymorphic variable (contd) When s is used to designate a Circle, the Circle is seen as a Shape, meaning that only the characteristics (methods and variables) of the class Shape can be used.

Shape s; Circle c; c = new Circle( 0, 0, 1 ); s = c; if ( s.getx() ) {... Polymorphic variable (contd) Here, s of type Shape, getx() is defined in the class Shape.

Shape s; Circle c; c = new Circle( 0, 0, 1 ); s = c; if ( s.getx() ) {... Polymorphic variable (contd) This makes sense, s can be used to designate objects of the class Shape or a subclass of Shape. This object has all the characteristics of a Shape.

Shape s; Circle c; c = new Circle( 0, 0, 1 ); s = c; if ( s.getradius() ) {... Polymorphic variable (contd) The above statement is not valid. Why? The method getradius() is not defined in the class Shape (or its parents).

Polymorphic variable (contd) 1) The type of a reference variable defines the set of classes whose objects could be designated by the reference. 2) The type of a reference variable defines the set of operations (method calls, access to instance variables, etc.) that are valid.

Polymorphism Polymorphism is a powerful concept. The method isleftof can be used to compare not only Circles and Rectangles but also any future subclass of Shape. public class Triangle extends Shape { //...

True polymorphism: motivation 2 Problem: write a method that compares the area of any two Shapes.

Absurd solution! Write methods with the same name and all four possible signatures (method overloading): (Circle, Circle), (Circle, Rectangle), (Rectangle, Circle) and (Rectangle, Rectangle). As many implementations as pairs of shapes! All the implementations are the same! Whenever a new kind of Shape is defined (say Triangle) then new methods compareto must be created!

What do you propose? How about this? public class Shape { //... Solution public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1;

Solution public class Shape { //... public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1; The above declaration would not compile! Why? Because, the superclass Shape does not have method area().

Solution Proposal? Let s create a dummy implementation of the method area(). public class Shape { //... // Must be redefined by the subclasses or else... public double area() { return -1.0; public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1;

Solution Too dangerous! The implementer of the subclass is not forced to redefined the method area(). public class Shape { //... // Must be redefined by the subclasses or else... public double area() { return -1.0; public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1;

Solution: abstract The solution is to declare the method area() abstract in the superclass Shape. An abstract method is declared using the keyword abstract, it has a signature but no body. public class Shape { //... public abstract double area(); // <---- public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1;

The above definition, alas, does not compile! Why?

Solution: abstract public class Shape { //... public abstract double area(); // <---- public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1; Imagine creating an object of the class Shape, that object would have a method area() that has no statements attached to it!

Solution: abstract class public abstract class Shape { // <--- //... public abstract double area(); // <---- public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1; A class that has an abstract method must be abstract. One cannot create an object of an abstract class! The statement new Shape() would cause a compile-time error.

Abstract classes A class that contains an abstract method (declared in that class or inherited) must be declared abstract; An abstract class cannot be used to create objects; A class that contains no abstract methods can also be declared abstract to prevent the creation of objects of this class. E.g. Employee, SalariedEmployee, HourlyEmployee.

Solution: abstract class What have we achieved? public class Triangle extends Shape { Triangle.java:1: Triangle is not abstract and does not override abstract method area() in Shape public class Triangle extends Shape { ^ 1 error It is now impossible to create a concrete subclass of Shape that has no method area()!

Solution: abstract methods and classes The declaration of an abstract method forces all the (concrete) subclasses to implement that method! public abstract class Shape { //... public abstract double area(); public int compareto( Shape other ) { if ( area() < other.area() ) return -1; else if ( area() == other.area() ) return 0; else return 1;

Late binding (a.k.a. dynamic binding, virtual binding) Account getuid() : double BankAccount getbalance() : double getmonthlyfees() : double ChequingAccount SavingAccount getmonthlyfees() : double Both classes BankAccount and SavingAccount are declaring a method getmontlyfees();

Let s say that the method getmonthlyfees of the class BankAccount always returns 25. public double getmonthlyfees() { return 25.0 The class SavingAccount overwrites this definition with the following. public double getmonthlyfees() { double result; if ( getbalance() > 5000 ) { result = 0.0; else { result = super.getmontlyfees(); return result;

Account a; BankAccount b; SavingAccount s; s = new SavingAccount(); s.getmontlyfees(); b = s; b.getmontlyfees(); a = b; a.getmontlyfees();

Dynamic Binding Let S (source) be the type of the object currently designated by a reference variable of type T (target). Unless the method is static or final, the lookup i) occurs at runtime and ii) starts at the class S: if the method is found, this is the method that will be executed, otherwise the immediate superclass is considered, this process continues until the first occurrence of the method is found. Sometimes called late or virtual binding.