index.pdf January 21,

Similar documents
ITI Introduction to Computing II

ITI Introduction to Computing II

ITI Introduction to Computing II

ITI Introduction to Computing II

ITI Introduction to Computing II

Java Object Oriented Design. CSC207 Fall 2014

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

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

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

Introduction to Programming Using Java (98-388)

VARIABLES AND TYPES CITS1001

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Polymorphism

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

Practice for Chapter 11

Inheritance, Polymorphism, and Interfaces

CS-202 Introduction to Object Oriented Programming

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

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

JAVA MOCK TEST JAVA MOCK TEST II

CS 251 Intermediate Programming Inheritance

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

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

CLASS DESIGN. Objectives MODULE 4

Object-Oriented Programming More Inheritance

Chapter 14 Abstract Classes and Interfaces

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

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

Example: Count of Points

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

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

Inheritance Motivation

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

OBJECT ORİENTATİON ENCAPSULATİON

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

Inheritance, polymorphism, interfaces

VIRTUAL FUNCTIONS Chapter 10

COURSE 2 DESIGN PATTERNS

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

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. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance, and Polymorphism.

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

Introduction to Inheritance

Advanced Placement Computer Science. Inheritance and Polymorphism

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

CS1150 Principles of Computer Science Objects and Classes

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

Introduction to Object-Oriented Programming

Lecture 5: Inheritance

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

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

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

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

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

G Programming Languages - Fall 2012

CS260 Intro to Java & Android 03.Java Language Basics

Industrial Programming

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

Inheritance and Polymorphism

More Relationships Between Classes

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

PROGRAMMING LANGUAGE 2

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

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

C18a: Abstract Class and Method

ITI Introduction to Computing II

Polymorphism and Interfaces. CGS 3416 Spring 2018

8. Polymorphism and Inheritance

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

C++ (classes) Hwansoo Han

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Inheritance and Polymorphism

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

CST242 Object-Oriented Programming Page 1

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

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

Methods Common to all Classes

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Transcription:

index.pdf January 21, 2013 1 ITI 1121. Introduction to Computing II Circle Let s complete the implementation of the class Circle. Marcel Turcotte School of Electrical Engineering and Computer Science Version of January 21, 2013 Where would you implement the method area()? In the class Shape or int the class Circle? Abstract Inheritance (part II) Polymorphism These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Circle public class Circle extends Shape { private double radius; public double getradius() { return radius; 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? public double area() { return Math.PI * radius * radius; public void scale( double factor ) { radius *= factor; 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

index.pdf January 21, 2013 2 Polymorphism Method overloading 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 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 that returns true if this Shape is to the left of its argument.

index.pdf January 21, 2013 3 Circle c1, c2; c1 = new Circle( 10, 20, 5 ); c2 = new Circle( 20, 10, 5 ); if ( c1.( c2 ) ) { System.out.println( "c1 c2" ); { System.out.println( "c2 c1" ); Rectangle r1, r2; r1 = new Rectangle( 0, 0, 1, 1 ); r2 = new Rectangle( 100, 100, 200, 400 ); if ( r1.( r2 ) ) { System.out.println( "r1 r2" ); { System.out.println( "r2 r1" ); if ( r1.( c1 ) ) { System.out.println( "r1 c1" ); { System.out.println( "c1 r1" ); if ( c2.( r2 ) ) { System.out.println( "c2 r2" ); { System.out.println( "r2 c2" ); Absurd solution! public boolean ( Circle c ) { return getx() < c.getx(); public boolean ( Rectangle r ) { return getx() < r.getx(); Why is that solution absurd? Absurd solution! public boolean ( Circle c ) { return getx() < c.getx(); public boolean ( Rectangle r ) { return getx() < r.getx(); As many implementations as kinds of shape! What do you propose? The method getx() is common to all the Shapes; all shapes have a getx(). public boolean ( Any Shape s ) { return getx() < s.getx(); How does one write Any Shape? All the implementations are the same! Whenever a new kind of Shape is defined (say Triangle) then a method ileftof must be created!

index.pdf January 21, 2013 4 Implement the method in the class Shape as follows. public boolean ( Shape s ) { return getx() < s.getx(); c = new Circle( 10, 20, 5 ); Rectangle r; r = new Rectangle( 0, 0, 1, 1 ); if ( c.( r ) ) { System.out.println( "c r" ); { System.out.println( "r c" ); if ( c.( r ) ) { The method of the object designated by c is called. Okay, c designates an object of the class Circle, which inherits the method. if ( c.( r ) ) { Hum, when the method 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!? 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: Based on that definition, the following statements are valid. Rectangle r; r = new Rectangle( 0, 0, 1, 1 ); s = r; but r = s is not! 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.

index.pdf January 21, 2013 5 Polymorphic variable Polymorphic method: true polymorphism The variable s designates any object that is from a subclass of Shape. Usage: s = new Circle( 0, 0, 1 ); s = new Rectangle( 10, 100, 10, 100 ); public boolean ( Shape other ) { boolean result; if ( getx() < other.getx() ) { result = true; { result = false; return result; Usage: Circle c = new Circle( 10, 10, 5 ); Rectangle d = new Rectangle( 0, 10, 12, 24 ); if ( c.( d ) ) {... if ( c.getx() ) {... // valid? if ( s.getx() ) {... // valid? The object designated by s is still a Circle. The class of object does not change during the execution of the program. if ( c.getradius() ) {... // valid? if ( s.getradius() ) {... // valid? if ( s.getx() ) {... 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. if ( s.getx() ) {... Here, s of type Shape, getx() is defined in the class Shape.

index.pdf January 21, 2013 6 if ( s.getx() ) {... 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. if ( s.getradius() ) {... The above statement is not valid. Why? The method getradius() is not defined in the class Shape (or its parents). 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 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!

index.pdf January 21, 2013 7 What do you propose? How about this? The above declaration would not compile! Why? Because, the superclass Shape does not have method area(). Proposal? Let s create a dummy implementation of the method area(). // Must be redefined by the subclasses or... public double area() { return -1.0; Too dangerous! The implementer of the subclass is not forced to redefined the method area(). // Must be redefined by the subclasses or... public double area() { return -1.0; : abstract The above definition, alas, does not compile! Why? 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 abstract double area(); // <----

index.pdf January 21, 2013 8 : abstract : abstract class public abstract double area(); // <---- Imagine creating an object of the class Shape, that object would have a method area() that has no statements attached to it! public abstract class Shape { // <--- public abstract double area(); // <---- 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. What have we achieved? : abstract class 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()! : 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(); 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();

index.pdf January 21, 2013 9 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; { 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.