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

Similar documents
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

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

! labs this week. ! midterms returned. ! work through what you got wrong on midterm. ! can earn back up to 5 out of 70 points

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

! Midterm 2: Thu Mar 16, 6:30pm (TODAY!) ! Woodward 2. ! hour-long exam, reserve 6:30-8 time slot. ! no labs/tutorials this week. ! Bunny.

ECE 122. Engineering Problem Solving with Java

Assignments. Material Covered. How To Prepare

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS 251 Intermediate Programming Inheritance

! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

Super-Classes and sub-classes

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

JAVA MOCK TEST JAVA MOCK TEST II

COMP 110/L Lecture 19. Kyle Dewey

Java Object Oriented Design. CSC207 Fall 2014

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

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

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

Inheritance -- Introduction

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

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

Abstract Classes and Interfaces

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

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

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

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

ECE 122. Engineering Problem Solving with Java

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

Java Fundamentals (II)

Inheritance and Interfaces

Introduction to Inheritance

Inheritance. Transitivity

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

Midterms Save the Dates!

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

COP 3330 Final Exam Review

More on Inheritance. Interfaces & Abstract Classes

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

Chapter 14 Abstract Classes and Interfaces

ITI Introduction to Computing II

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

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Overriding Variables: Shadowing

C++ Important Questions with Answers

ITI Introduction to Computing II

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Midterms Save the Dates!

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

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

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

Inheritance and Polymorphism. CS180 Fall 2007

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

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

Inheritance, Polymorphism, and Interfaces

Programming Exercise 14: Inheritance and Polymorphism

ECE 122. Engineering Problem Solving with Java

Building custom components IAT351

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

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

CS-202 Introduction to Object Oriented Programming

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

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

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

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

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

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

Midterms Save the Dates!

COMP 250 Fall inheritance Nov. 17, 2017

First IS-A Relationship: Inheritance

Inheritance and Interfaces

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

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

Java Programming Lecture 7

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

Programming in C# Inheritance and Polymorphism

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

CSE 113 A. Announcements - Lab

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

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

CMSC 132: Object-Oriented Programming II

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

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

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

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

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CS111: PROGRAMMING LANGUAGE II

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

Data Abstraction. Hwansoo Han

INHERITANCE. Spring 2019

More Relationships Between Classes

Chapter 6 Reflection

Big software. code reuse: The practice of writing program code once and using it in many contexts.

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

Lecture 18 CSE11 Fall 2013 Inheritance

Transcription:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Inheritance II Lecture 23, Thu Mar 30 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr 1

Check your lab 7 grade News we haven t yet handed out midterm solution, but the window will close soon! 5/70 midterm points is 1% of your course grade! Yet a few more (but not all) Assignment 2s to hand back after class Assignment 3 due Friday Apr 7, 5pm start now now now! Final exam: Mon Apr 24, 3:30pm, HEBB TH Evaluations today (beginning of class) 2

Recap: Comparable sort method that works on array of objects of any type that implements Comparable type guaranteed to have compareto method sorted int String Bunny revisit Bunny.compareTo: checking dynamic type of object 3

Recap: Multiple Interfaces Classes can implement more than one interface at once contract to implement all abstract methods defined in every interface it implements public class MyClass implements Interface1, Interface2, Interface3 4

Recap: Inheritance Inheritance: process by which new class is derived from existing one fundamental principle of object-oriented programming Create new child class (subclass) that extends existing parent one (superclass) inherits all methods and variables except constructor can just add new variables and methods 5

Recap: Inheritance and Constructors public class 2000 extends 2 public 2000() super(); public 2000(int n) super(n); public void load(int n) numberofcans = numberofcans + n; System.out.println("Adding " + n + " cans to this machine"); Subclass (child class) inherits all methods except constructor methods from superclass (parent class) Using reserved word super in subclass constructor tells Java to call appropriate constructor method of superclass 6

Recap: Inheritance and Scope Subclasses inherits but cannot directly access private fields or variables of superclass Protected variables can be directly accessed from declaring class and any classes derived from it 7

Recap: Method Overriding If child class defines method with same name and signature as method in parent class say child's version overrides parent's version in favor of its own 8

Recap: Object Behind the Scenes All classes that aren't explicitly extended from a named class are by default extended from Object class Object class includes a tostring() method so... class header public class myclass is actually same as public class myclass extends Object 9

Recap: Overriding Variables You can, but you shouldn't Possible for child class to declare variable with same name as variable inherited from parent class one in child class is called shadow variable confuses everyone! Child class already can gain access to inherited variable with same name there's no good reason to declare new variable with the same name 10

Recap: Method Overloading and Overriding Method overloading: "easy" polymorphism in any class can use same name for several different (but hopefully related) methods methods must have different signatures so that compiler can tell which one is intended Method overriding: "complicated polymorphism subclass has method with same signature as a method in the superclass method in derived class overrides method in superclass resolved at execution time, not compilation time some call it true polymorphism 11

Objectives Understanding when and how to use abstract classes Understanding tradeoffs between interfaces and inheritance 12

A New Wrinkle Expand vending machine empire to include French fry machines is a French fry machine a subclass of? 13

If We Have This Class Hierarchy... 2000 UA 14

...Does This Make Sense? French Fry 2000 UA 15

Does This Make More Sense? Generic Vending French Fry 2000 UA 16

Does This Make More Sense? French Fry Generic Vending Yes especially if we're thinking of adding all kinds of vending machines... 2000 UA 17

Does This Make More Sense? Generic Vending Beer French Fry Pizza Yes especially if we're thinking of adding all kinds of vending machines... want our classes to be more specific as we go down class hierarchy is French Fry more or less specific than? neither, both specific versions of generic Vending class 2000 UA 18

Does This Make More Sense? Generic Vending Beer French Fry Pizza One way: make a Vending interface like last week Another way... 2000 UA 19

Inheritance Solution public class GenericVending private int numberofitems; private double cashin; public GenericVending() numberofitems = 0; public boolean venditem() boolean result; if (numberofitems > 0) numberofitems--; result = true; else result = false; return result; 20

Inheritance Solution public void loaditems(int n) numberofitems = n; public int getnumberofitems() return numberofitems; 21

Inheritance Solution public class 3 extends GenericVending public 3() super(); public 3(int n) super(); this.loaditems(n); public void buy() if (this.venditem()) System.out.println("Have a nice frosty Coca-Cola!"); System.out.println(this.getNumberOfItems() + " cans of remaining"); else System.out.println("Sorry, sold out"); 22

Inheritance Solution public void load(int n) this.loaditems(this.getnumberofitems() + n); System.out.println("Adding " + n + " ice cold cans of to this machine"); 23

Inheritance Solution public class 2000 extends 3 public 2000() super(); public 2000(int n) super(); this.loaditems(n); public void load(int n) super.load(n); System.out.println("Loading in the new millennium!"); 24

Inheritance From Generic Objects French Fry Generic Vending Want generic Vending class don t actually use to generate objects use as template for specific actual classes like FrenchFry and 2000 UA 25

Inheritance From Generic Objects Generic Vending Beer French Fry Pizza Will we ever want to instantiate a generic Vending class? will we ever need to make generic Vending object? 2000 UA 26

Inheritance From Generic Objects Generic Vending Beer French Fry Pizza Will we ever want to instantiate a generic Vending class? will we ever need to make generic Vending object? No, not in our simulated vending world! How would we use one? What would be a real-life equivalent? 2000 UA 27

Inheritance From Generic Objects Introduced UA to combat vandalism and theft Could just add vandalize() methods to CM, CM2000, CMUA but we want to ensure that all Vending s have vandalize() methods want all of them to be different if put into base class at top, easy to have them identical no way to force method overriding 28

Abstract Classes Abstract class: not completely implemented Usually contains one or more abstract methods has no definition: specifies method that should be implemented by subclasses just has header, does not provide actual implementation for that method Abstract class uses abstract methods to specify what interface to descendant classes must look like without providing implementation details for methods that make up interface Example: require that all subclasses of Vending class implement vandalize() method method might differ greatly between one subclass and another use an abstract method 29

Abstract Classes Abstract classes serve as place holders in class hierarchy Abstract class typically used as partial description inherited by all its descendants Description insufficient to be useful by itself cannot instantiated if defined properly Descendent classes supply additional information so that instantiation is meaningful abstract class is generic concept in class hierarchy class becomes abstract by including the abstract modifier in class header 30

Abstract Classes Use abstract class for generic template can use abstract methods Making abstract method Use restricted word abstract in method header do not provide a method body just end method header with semicolon 31

Vending Class Revisited public abstract class Vending private int numberofitems; public Vending() numberofitems = 0; public boolean vend() boolean result; if (numberofitems > 0) numberofitems--; result = true; else result = false; return result; public abstract void vandalize(); 32

Abstract Methods and Abstract Classes What happens when we try to compile it all now? Java tells us that there's an abstract class we have to implement 33

Abstract Methods and Abstract Classes What happens when we try to compile it all now? Java tells us that there's an abstract class we have to implement Could put this class: public void vandalize() System.out.println("Take all my money, and have a too"); 34

Abstract Methods and Abstract Classes What happens when we try to compile it all now? Java tells us that there's an abstract class we have to implement Could put this class: public void vandalize() System.out.println("Take all my money, and have a too"); Do we have to implement method in 2000 and UA classes too? Yes, if we want them to behave differently when they're vandalized original intent 35

Which Organization? implements implements Generic Vending implements implements Beer French Fry Pizza extends 2000 extends UA 36

Which Organization? extends extends Generic Vending extends extends Beer French Fry Pizza extends 2000 extends UA 37

Interfaces vs. Abstract Classes If we can have abstract class that contains only abstract methods, why do we need interfaces? 38

Interfaces vs. Abstract Classes If we can have abstract class that contains only abstract methods, why do we need interfaces? Java does not support multiple inheritance: child classes inheriting attributes from multiple parent classes other object-oriented languages do multiple inheritance can be good, but causes problems what if child class inherits two different methods with same signature from two different parents? which one should be used? 39

Interfaces vs. Abstract Classes Java's formal interface provides some of the utility of multiple inheritance without the problems class can implement more than one interface can do this at same time it extends class Interface allows us to create classes that "inherit" features from multiple places 40

Interfaces vs. Abstract Classes Java's formal interface provides some of the utility of multiple inheritance without the problems class can implement more than one interface can do this at same time it extends class Interface allows us to create classes that "inherit" features from multiple places Why is problem from previous slide solved? might have multiple method headers with same signature 41

Interfaces vs. Abstract Classes Java's formal interface provides some of the utility of multiple inheritance without the problems class can implement more than one interface can do this at same time it extends class Interface allows us to create classes that "inherit" features from multiple places Why is problem from previous slide solved? might have multiple method headers with same signature but only one will have an actual definition no ambiguity on which will be used but still could be problem with different return types 42

Interfaces vs. Abstract Classes Another useful feature provided by interfaces: inheritance happens between classes that are related But classes can implement completely unrelated interfaces and that can be useful 43

Interfaces vs. Abstract Classes Another useful feature provided by interfaces: inheritance happens between classes that are related But classes can implement completely unrelated interfaces and that can be useful Example: implement interfaces for computer, printer, cell phone, vending machine create class for new interactive vending machines that: vend s, show annoying music videos, phone their owner when they're running low on product, and spit out coupons for free prizes 44

How Interfaces Differ From Abstract Classes Abstract class is incomplete class that requires further specialization interface is just specification or prescription for behavior from Just Java 2 by Peter van der Linden 45

How Interfaces Differ From Abstract Classes Abstract class is incomplete class that requires further specialization interface is just specification or prescription for behavior Inheritance implies specialization, interface does not interface just implies "We need something that does 'foo' and here are ways that users should be able to call it. from Just Java 2 by Peter van der Linden 46

How Interfaces Differ From Abstract Classes Abstract class is incomplete class that requires further specialization interface is just specification or prescription for behavior Inheritance implies specialization, interface does not interface just implies "We need something that does 'foo' and here are ways that users should be able to call it. Class can implement several interfaces at once but class can extend only one parent class from Just Java 2 by Peter van der Linden 47

Interfaces vs. Abstract Classes: Bottom Line Use abstract class to initiate a hierarchy of more specialized classes Use interface to say, "I need to be able to call methods with these signatures in your class." Use an interface for some semblance of multiple inheritance from Just Java 2 by Peter van der Linden 48

Interfaces vs. Abstract Classes Interface can only extend another interface cannot extend abstract class or "concrete" class Class can legally implement only some methods of interface if it s abstract class then must be further extended through inheritance before can be instantiated from Just Java 2 by Peter van der Linden 49

Who Can Do What? Interface can be implemented only by class or abstract class Interface can be extended only by another interface Class can be extended only by class or abstract class Abstract class can be extended only by class or abstract class Only classes can be instantiated as objects Interfaces are not classes and cannot be instantiated Abstract classes may have undefined methods and cannot be instantiated 50