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

Similar documents
Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

Java Object Oriented Design. CSC207 Fall 2014

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

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

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

CS260 Intro to Java & Android 03.Java Language Basics

More On inheritance. What you can do in subclass regarding methods:

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

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

Introduction to Programming Using Java (98-388)

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Rules and syntax for inheritance. The boring stuff

Binghamton University. CS-140 Fall Dynamic Types

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

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

} Each object in a Java program has an identifier (name) } This includes:

Multiple Inheritance, Abstract Classes, Interfaces

Logistics. Final Exam on Friday at 3pm in CHEM 102

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

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

What is Inheritance?

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

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Super-Classes and sub-classes

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)

Inheritance (Outsource: )

INHERITANCE AND EXTENDING CLASSES

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

Data Structures (list, dictionary, tuples, sets, strings)

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

CLASS DESIGN. Objectives MODULE 4

ITI Introduction to Computing II

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

APCS Semester #1 Final Exam Practice Problems

The class Object. Lecture CS1122 Summer 2008

Programming Exercise 14: Inheritance and Polymorphism

CS171:Introduction to Computer Science II

ITI Introduction to Computing II

COP 3330 Final Exam Review

Inheritance - Assignment5

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Using System.out.println()

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

C++ Important Questions with Answers

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

Abstract Classes and Interfaces

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

What are the characteristics of Object Oriented programming language?

Java Magistère BFA

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

CS 11 java track: lecture 1

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Inheritance and Interfaces

ITI Introduction to Computing II

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

MIT AITI Lecture 18 Collections - Part 1

Making New instances of Classes

ITI Introduction to Computing II

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

Practice for Chapter 11

Chapter 9 Inheritance

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Data Structures and Other Objects Using C++

Object Oriented Programming. Java-Lecture 11 Polymorphism

What is an algorithm?

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Announcements. Written Assignment 2 Due Monday at 5:00PM. Midterm next Wednesday in class, 11:00 1:00. Midterm review session next Monday in class.

Inheritance -- Introduction

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

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

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

CS 231 Data Structures and Algorithms, Fall 2016

CS 101 Spring 2006 Final Exam Name: ID:

index.pdf January 21,

CS11 Introduction to C++ Fall Lecture 7

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Inheritance. Exploring Polymorphism. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS-202 Introduction to Object Oriented Programming

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

Inheritance & Polymorphism

F I N A L E X A M I N A T I O N

Operators and Expressions

CISC370: Inheritance

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CS 177 Week 15 Recitation Slides. Review

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

TeenCoder : Java Programming (ISBN )

Tutorial 2 Today we will have a quick introduction to ArgoUML and how to use it, as well as look at some examples of class diagrams.

Practice Midterm 1. Problem Points Score TOTAL 50

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Transcription:

Review: Diagrams for Inheritance - String makemodel - int mileage + (String, int) Class #3: Inheritance & Polymorphism Software Design II (CS 220): M. Allen, 25 Jan. 18 + (String, int) <<update>> + void setmakemodel(string s) + void setmileage(int m) <<query>> + String getmakemodel() + int getmileage() int capacity + (String, int, int) <<update>> + void setcapacity(int c) <<query>> + int getcapacity() 2 Inheritance Structures s can be part of an inheritance hierarchy, with multiple levels of ancestors and descendants Every class in Java is descended from the class Type Conformance Every object conforms to the types of all its ancestors Every class in Java conforms to the type is a descendant of conforms to is an ancestor of and and conform to both and 3 4 1

Conformance Rules Any sub-class that conforms to another class can be used anywhere something of the original type can be used For example, if we have this method declaration: private void methodname( type1 var1, type2 var2 ) And then use it: object1 must conform to type1 methodname( object1, object2 ); object2 must conform to type2 Conformance Rules Often, we use strict conformance in our code For example, if we have this method declaration: public void setbounds( int x, int y, int w, int h ) We must use it with exactly the same types of inputs, since these are primitive types, with no conformance: setbounds( 20, 20, 100, 100 ); Conformance allows us to relax this requirement, using descendant classes instead 5 6 Using Conformance Once we have a set of sub-classes of a main class, we can do things like create an array of different objects Using Conformance Now that we have multiple types of in the array, we can write a method that works with multiple types at once v0, v1, v2, v3, v4; v0 = new ("Generic ", 0); v1 = new ("Toyota Prius", 100); v2 = new ("Honda Insight", 5000); v3 = new ("Mack ", 300, 20); v4 = new ("Ford F250", 1300, 2); [] lot = new [4]; lot[0] = v1; lot[1] = v2; lot[2] = v3; lot[3] = v4; variables get different types dynamically when created at run-time array now (appears to) have references to 3 different types of objects in it lot = new [5];... private void prints() System.out.println( lot[i].getmakemodel() ); Since any element of array conforms to the class, it will have access to the inherited method getmakemodel() 7 8 2

Conformance and Type-Checking Since we have declared the array of type, anything we try to do with objects in that array must be possible for any type that conforms to the class lot = new [5];... ERROR! private void prints() System.out.println( lot[i].getcapacity() ); Determining Specific Types Sometimes, we want to know something more specific about an object s type than we can know from the type of a variable or containing data-structure Java has instanceof operator to check conformance A boolean (binary, infix) operator Takes an object identifier and a type identifier as arguments Returns true if and only if the object conforms to the type Since only objects, specifically, have access to the method getcapacity(), this code will not compile 9 10 The instanceof Operator instanceof : true instanceof : true instanceof : false instanceof : true instanceof : false instanceof : false 11 Safe Casting of Conformant Types Once we know that an object conforms to some more specific type, we can use techniques like casting to use methods involving that type private void printcapacities() if ( lot[i] instanceof ) t = () lot[i]; System.out.println( t.getcapacity() ); else System.out.println( "Not a truck." ); Since we have used instanceof to check for the type, we can safely cast. Doing so will allow the object to call methods that do not belong to the more generic type. If we don t do the check first, the code will still compile, but the cast may fail at run-time! 12 3

Overriding Methods If we want to replace a method in a parent class we can create a new one to override it Must have same method signature: exact same access, name and list of parameters for input or output For example if the parent contains a method: public void dox( int y ) The child can override it by declaring own method with: 1. Same name: dox() 2. Same parameter type: int 3. Same output type: void 4. Same access: public Overriding Methods For instance, we might decide to override the parent method for setting the mileage, which allows any integer value we want: public void setmileage( int m ) mileage = m; We put a new version in the class, ensuring that the value is always between 0 and 500,000 Since the mileage public void setmileage( int m ) variable is private in if ( m < 0 ) the parent class, the m = 0; child version must else if ( m > 500_000 ) call the public method m = 500_000; that allows changing super.setmileage( m ); that variable, after setting the bounds on the value as desired 13 14 Types and Polymorphism Polymorphism: fr. Greek πολύμορῄοσ (polumorphos), meaning many shapes When you declare a variable, you assign its type As code runs, a polymorphic variable can appear to change type, based on objects assigned to it In each case, the compiler will check if the type of the assigned object conforms to that of the variable At runtime, the current type of the variable decides what methods and properties it will have 15 Polymorphism in Action A variable can be made to change type, since it is a reference that can be directed towards object of any conformant sub-type When we run a method on such a variable, it uses the version of any method provided by the actual type of the object to which it refers v0 = new ( "Generic, 0 ); v0.setmileage( -100 ); System.out.println( v0.getmakemodel() + ": " + v0.getmileage() ); v0 = new ( "Ford Focus", 100 ); v0.setmileage( -100 ); System.out.println( v0.getmakemodel() + ": " + v0.getmileage() ); Output: Generic : -100 Ford Focus: 0 On the first call to setmileage(), the actual type is, and we get the negative value (-100) On the second call to setmileage(), the actual type is now, and we get the minimum-bound value (0) 16 4

This Week Topic: Inheritance and polymorphism Class meetings: Monday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Optional Reading: Chapter 5 & 6 in text Office Hours: Wing 210 Monday/Wednesday/Friday, 10:00 AM 11:00 AM Tuesday/Thursday, 1:30 PM 3:00 PM 17 5