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

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

ECE 122. Engineering Problem Solving with Java

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

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

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

Sept 26, 2016 Sprenkle - CSCI Documentation is a love letter that you write to your future self. Damian Conway

Java Object Oriented Design. CSC207 Fall 2014

Chapter 6 Reflection

ECE 122. Engineering Problem Solving with Java

CS1004: Intro to CS in Java, Spring 2005

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

&KDSWHU(QKDQFLQJ&ODVVHV

Chapter 5: Enhancing Classes

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

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

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

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

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

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

Inheritance -- Introduction

CS 251 Intermediate Programming Inheritance

Inheritance and Polymorphism

Introduction to Inheritance

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Polymorphism. Agenda

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Chapter 10 INHERITANCE 11/7/16 1

S <: T Type compa*bility rela*on.

What is Inheritance?

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

Encapsula)on, cont d. Polymorphism, Inheritance part 1. COMP 401, Spring 2015 Lecture 7 1/29/2015

INHERITANCE AND EXTENDING CLASSES

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

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

CS-202 Introduction to Object Oriented Programming

Subclassing, con.nued Method overriding, virtual methods, abstract classes/methods. COMP 401, Spring 2015 Lecture 9 2/19/2015

JAVA MOCK TEST JAVA MOCK TEST II

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

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited

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

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

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

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

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Inheritance. Transitivity

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

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

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

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

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

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS304 Object Oriented Programming Final Term

Chapter 14 Abstract Classes and Interfaces

CLASS DESIGN. Objectives MODULE 4

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

Chapter 10 Classes Continued. Fundamentals of Java

Object-Oriented Programming More Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

More about inheritance

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Inheritance (Outsource: )

Arrays Classes & Methods, Inheritance

Create a Java project named week10

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

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Chapter 4: Writing Classes

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

INTERFACE WHY INTERFACE

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Computer Science II (20073) Week 1: Review and Inheritance

Inheritance. A key OOP concept

Programming II (CS300)

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

Programming Exercise 14: Inheritance and Polymorphism

The class Object. Lecture CS1122 Summer 2008

First IS-A Relationship: Inheritance

TeenCoder : Java Programming (ISBN )

More Relationships Between Classes

Chapter 5. Inheritance

Interfaces CSC 123 Fall 2018 Howard Rosenthal

C++ Inheritance and Encapsulation

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

CMSC 132: Object-Oriented Programming II

Polymorphism. return a.doublevalue() + b.doublevalue();

OVERRIDING. 7/11/2015 Budditha Hettige 82

Introduc)on to Classes and Objects. Dr. Bowen Hui Computer Science University of Bri)sh Columbia Okanagan

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Inheritance, Polymorphism, and Interfaces

Building custom components IAT351

QUESTIONS FOR AVERAGE BLOOMERS

ECE 122. Engineering Problem Solving with Java

ITI Introduction to Computing II

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

Inheritance and Polymorphism

Transcription:

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

Quick Review Inheritance models IS- A rela?onship Different from impor?ng classes Inherited classes can be organized in a class hierarchy Special classes to model generic concepts that do not get instan?ated are called abstract classes 2

Abstract Class Example public abstract class Animal { protected int numlegs; protected int speed; public abstract void walks(); public abstract void eats(); public int runs() { // statements to define how fast it runs based on speed } } 3

Abstract Class Example public abstract class Animal { protected int numlegs; protected int speed; public abstract void walks(); public abstract void eats(); no constructor public int runs() { // statements to define how fast it runs based on speed } } Abstract methods end with semicolon All subclasses must define walks() and eats() or declare them abstract too 4

Problem of Mul?ple Inheritance Recall Java does not allow this: Vehicle Car Truck PickupTruck 5

Problem of Mul?ple Inheritance Recall Java does not allow this: Car inherits from Vehicle Truck inherits from Vehicle Vehicle Car Truck PickupTruck cannot inherit from more than one class PickupTruck 6

Origin of Interfaces An OOP programming technique that lets you conform to mul?ple classes Avoids the collision problem that arises in mul?ple inheritance Don t have any a\ributes A Java interface is a group of constants and abstract methods All methods in an interface must be abstract Reserved word abstract is not needed 7

Example Example of an interface: public interface Doable { public void dothis(); public int dothat(); public void dothis2( double value, char ch ); public boolean dotheother( int num ); } 8

Example Example of an interface: public interface Doable { no constructor public void dothis(); public int dothat(); public void dothis2( double value, char ch ); public boolean dotheother( int num ); } none of the methods have body defini?ons What does this example remind you of? 9

Using an Interface Suppose you want to define different types of memory storage components e.g., CD, USB key, etc. You have: 10

Implemen?ng an Interface 11

Implemen?ng an Interface can have a\ributes have constructor all abstract methods must be defined 12

About Interfaces An interface cannot be instan?ated All methods are public abstract Even if reserved words not used All methods must be defined by classes that implement the interface To implement an interface: Use reserved word implements Define all the methods from the interface Can define addi?onal a\ributes and methods 13

About Interfaces An interface cannot be instan?ated All methods are public abstract Even if reserved words not used All methods must be defined by classes that implement the interface To implement an interface: Use reserved word implements Define all the methods from the interface Can define addi?onal a\ributes and methods 14

Interfaces vs. Abstract Classes Interfaces: No a\ributes All method are abstract All methods are public Abstract classes: Can have a\ributes Can have methods with body defini?ons Methods can have different visibility 15

Interfaces vs. Abstract Classes Interfaces: No a\ributes All method are abstract All methods are public Abstract classes: Can have a\ributes Can have methods with body defini?ons Methods can have different visibility 16

When to Use Which? Interfaces: Want to guarantee another class will implement a set of methods Choose when: want to specify method signatures to enforce compliance Designed for standardizing communica?on across classes Abstract classes: No guarantee a subclass will override an abstract method Choose when: want subclasses to have a default behaviour Designed for conceptual modeling and to maximize reuse 17

Predefined Interfaces Interfaces other people defined Comparable interface Contains one abstract method called compareto() Used to compare two objects How to call it: o1.compareto( o2 ) where o1 and o2 are the same types Looks just like any other method call with an object passed in 18

The Comparable Interface Any class can implement Comparable Output type: int Output is nega?ve if obj1 is less than obj2 0 if the two are equal posi?ve if obj1 is greater than obj2 E.g.: if( obj1.compareto( obj2 ) < 0 ) System.out.println( obj1 is less than obj2 ); Your class gives meaning to less/greater than 19

String implements Comparable Recall from Ch 5 that String class lets us compare strings by lexicographic order E.g., abc < bcd You write: String one = abc ; String two = bcd ; if( one.compareto( two ) < 0 ) System.out.println( one + is less than + two ); 20

Defining compareto() Required header defini?on: public int compareto( Object o2 ) How come we were able to write: one.compareto( two ) where one and two are Strings? Makes use of polymorphism (next class) Inside compareto(), need to cast types Explicitly tells Java what type it really is E.g., for the String class, you would have: public int compareto( Object o2 ) { String other = ( String )o2; } 21

Defining compareto() Required header defini?on: public int compareto( Object o2 ) How come we were able to write: one.compareto( two ) where one and two are Strings? Makes use of polymorphism (next class) Inside compareto(), need to cast types Explicitly tells Java what type it really is E.g., for the String class, you would have: public int compareto( Object o2 ) { String other = ( String )o2; } 22

Defining compareto() Required header defini?on: public int compareto( Object o2 ) How come we were able to write: one.compareto( two ) where one and two are Strings? Makes use of polymorphism (next class) Inside compareto(), need to cast types Explicitly tells Java what type it really is E.g., for the String class, you would have: public int compareto( Object o2 ) { String other = ( String )o2; } 23

Defining compareto() Required header defini?on: public int compareto( Object o2 ) How come we were able to write: one.compareto( two ) where one and two are Strings? Makes use of polymorphism (next class) Inside compareto(), need to cast types Explicitly tells Java what type it really is E.g., for the String class, you would have: public int compareto( Object o2 ) { String other = ( String )o2; } 24

Defining compareto() Required header defini?on: public int compareto( Object o2 ) How come we were able to write: one.compareto( two ) where one and two are Strings? Makes use of polymorphism (next class) Inside compareto(), need to cast types Explicitly tells Java what type it really is E.g., for the String class, you would have: public int compareto( Object o2 ) { String other = ( String )o2; } says o2 really is a String type 25

Shoe Size Comparison Compare two shoe sizes based on the following chart: Note: Men sizes are +2 to women s 26

Define Shoe class Which a\ributes will you keep track of? size gender Which methods do you need? Shoe() compareto() How to access other object s private a\ributes? getsize() getgender() 27

Define Shoe class Which a\ributes will you keep track of? size gender Which methods do you need? Shoe() compareto() How to access other object s private a\ributes? getsize() getgender() 28

Define Shoe class Which a\ributes will you keep track of? size gender Which methods do you need? Shoe() compareto() How to access other object s private a\ributes? getsize() getgender() 29

Define Shoe class Which a\ributes will you keep track of? size gender Which methods do you need? Shoe() compareto() How to access other object s private a\ributes? getsize() getgender() 30

Sample Solu?on 31

Sample Solu?on (cont.) 32

Sample Solu?on (cont.) 33

Tes?ng Your Solu?on Solu?on has lots of comparisons Set up various Shoe objects Compare them: Write down expected output (based on your own logic in the comments) Display expected output via println() Are they the same? Make sure to check all your comparisons 34

Tes?ng the Sample Solu?on 35

Interface Hierarchies An interface can inherit from another interface Example: public interface Sports { public void sethometeam( String name ); public void setvisitingteam( String name ); } public interface Volleyball extends Sports { public void sethometeamscore( int points ); public void setvisitingteamscore( int points ); } A class that implements Volleyball needs to define how many methods? 36

Interface Hierarchies A child interface inherits all abstract methods from the parent A class implemen?ng the child interface must therefore define all methods from both interfaces Class hierarchies and interface hierarchies don t overlap A class cannot extends an interface A class can only implements an interface 37

Mul?ple Interfaces A class can implement mul?ple interfaces public class ManyThings implements interface1, interface2 { // all methods of both interfaces } All listed in the same implements clause 38

Simple Example 39

Simple Example 40

Simple Example 41

Combining extends and implements Recall origin of interfaces: a way to get around not having mul?ple inheritance A class can extend another class and implement yet other classes Example: PickupTruck inherits from Truck PickupTruck implements Car 42

PickupTruck Example 43

Summary of Interface Concepts Interface makes classes conform to a communica?on standard All methods are abstract Can include constants defini?ons Cannot be instan?ated No a\ributes New reserved word: implements Interfaces can extend other interfaces to form an interface hierarchy All abstract methods are inherited Overcomes not having mul?ple inheritance Comparable interface comes with compareto() 44

Admin Next class: Prac?ce between inheritance and interfaces Use them (both) in polymorphism aper that Also use arrays and loops finish A1 Q1 by Thursday Labs next week: Prac?ce inheritance 45