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

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

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

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

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

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

Midterms Save the Dates!

Midterms Save the Dates!

Last Week: Organizing Data. Last Week: Parallel Arrays. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Survey #3. Final Exam. Today is totally optional! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings.

Midterms Save the Dates!

Midterms Save the Dates!

Inheritance and Polymorphism

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

Midterms Save the Dates!

Midterms Save the Dates!

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

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

Inheritance and Polymorphism

CSC207 Week 3. Larry Zhang

COMP 110/L Lecture 19. Kyle Dewey

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

CS18000: Problem Solving And Object-Oriented Programming

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

ECE 122. Engineering Problem Solving with Java

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Java Object Oriented Design. CSC207 Fall 2014

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Darrell Bethea June 6, 2011

Basic Object-Oriented Concepts. 5-Oct-17

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

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

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

Building custom components IAT351

Inheritance. Transitivity

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Week 3 Classes and Objects

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

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

Create a Java project named week10

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

Midterms Save the Dates!

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

Introduction to Inheritance

Lecture 18 CSE11 Fall 2013 Inheritance

CS111: PROGRAMMING LANGUAGE II

Midterms Save the Dates!

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

CS111: PROGRAMMING LANGUAGE II

CSC209. Software Tools and Systems Programming.

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Everything is an object. Almost, but all objects are of type Object!

MITOCW watch?v=flgjisf3l78

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

What is Inheritance?

Inheritance and Interfaces

type conversion polymorphism (intro only) Class class

Midterms Save the Dates!

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

Exercise: Singleton 1

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

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

Inheritance (Part 5) Odds and ends

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

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

CMSC 341. Nilanjan Banerjee

COMP 110/L Lecture 20. Kyle Dewey

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Programming Exercise 14: Inheritance and Polymorphism

CSE 113. Announcements

Abstract Classes and Interfaces

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

Rules and syntax for inheritance. The boring stuff

Classes and Inheritance Extending Classes, Chapter 5.2

Asking For Help. BIT 115: Introduction To Programming 1

BM214E Object Oriented Programming Lecture 11

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

The class Object. Lecture CS1122 Summer 2008

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

INHERITANCE AND EXTENDING CLASSES

CSE 143 Lecture 12 Inheritance

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

CSE : Python Programming

Java Classes, Inheritance, and Interfaces

16 Multiple Inheritance and Extending ADTs

CPSC 324 Topics in Java Programming

QUIZ. How could we disable the automatic creation of copyconstructors

5.6.1 The Special Variable this

Distributed Systems Recitation 1. Tamim Jabban

1.00 Lecture 14. Exercise: Plants

CPSC Tutorial 4

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Accessing the Superclass Object Hierarchies is-a, has-a Readings This Week: Ch 9.4-9.5 and into Ch 10.1-10.8 (Ch 11.4-11.5 and into Ch 13 in old 2 nd ed) Next Week: No new readings. Consolidate! (Reminder: Readings are absolutely vital for learning this stuff!) Lecture 21 Labs and Tutorials This Week: Lab #9 Next Week: Lab #10 (up on WebCT soon) Survey #2 Dr. Ben Yu s second survey for you all is up on WebCT now. Completely optional But, there s a bit of extra credit for doing all three surveys by their respective deadlines! The deadline for this one is March 29. Because of the server crash, if you did this survey before the crash, you ll have to do it again. Final Exam Wednesday, April 15, 7pm in SRC A This wasn t a good room last year, but we re stuck with the date, time, and room UBC assigns. Programming Assignment 3 Assignment 3 is up on WebCT! Click on the Assignments icon. Assigned Sunday evening sorry for delay! Due at NOON, April 6 (Monday), via electronic hand in. Tips: There is some Eclipse setup. Set-up ASAP! Work in pairs. Some conceptual stuff. Think carefully before coding. If concepts right, the coding is much much easier. 1

Programming Assignment 3 I found out after class on Monday that some versions of Vista and Java don t quite get along for some graphics programs. I ve spent the last couple days pinning this down. My initial guesses weren t correct. I have a fix now that works for several people. See the assignment webpage or WebCT for details. Learning Goals By the end of class today you will be able to Access methods and fields in the superclass from the subclass. Decide when it s good to inherit from another class and when it s not so good. Explain the is-a and has-a relationships. Last Time: Inheritance in Java You can declare a new class as an extension of an existing one. The new class automatically inherits all the instance fields and methods of the old class. The new class can add/change fields and methods. public class ChildClass extends ParentClass { put any additional fields and methods here Last Time: Silly Inheritance Example Silly Inheritance Example Hey, UBCStudent and TalkingDoll both declare a private instance field called name. Let s eliminate the duplicate code Accessing the Superclass Even though the subclass has all the instance fields and methods of the superclass, Java still thinks of it as a separate class. So, the subclass can t directly access anything private! 2

Accessing the Superclass Even though the subclass has all the instance fields and methods of the superclass, Java still thinks of it as a separate class. So, the subclass can t directly access anything private! What do you do? Use the public accessor/mutator methods. Have the superclass make things public. (Have the superclass make things protected.) Overriding You can declare a new class as an extension of an existing one. The new class automatically inherits all the instance fields and methods of the old class. The new class can add/change fields and methods. public class ChildClass extends ParentClass { anything new here is added to the ChildClass anything with same signature as in ParentClass overrides the ParentClass Recap: Variable Scope and Shadowing Scope tells you which declarations you can see from which points in the program. The scope of a variable is the places in the program where the variable can be accessed. Or you can think of scope as the places where you can see some variable. Shadowing and Overriding Shadowing and Overriding Shadowing public class foo { String name; public void setname(string name) { // name is shadowed! this.name = name; Shadowing and Overriding Overriding public class bar { public void a(double b) { public class foo extends bar { public void a(double b) { // override! 3

Shadowing and Overriding Overriding public class bar { public void a(double b) { public class foo extends bar { public void a(int b) { // no override Shadowing and Overriding Overriding public class bar { public void a(double b) { public class foo extends bar { public void a(double x) { // override? super and this The keyword super works just like the keyword this to get around the shadowing. this means the current object (implicit parameter) super means the superclass object. Using super to Avoid Overriding Overriding public class bar { public void a(double b) { public class foo extends bar { public void a(double b) { // override! super.a(3.14); // bypasses override Special Case: Superclass Constructor Recall the special use of this in constructors? this as first line of constructor calls a different constructor for the same object E.g., UBCStudent t class Special Case: Superclass Constructor Recall the special use of this in constructors? this as first line of constructor calls a different constructor for the same object E.g., UBCStudent t class Similar trick to call a superclass constructor using the super keyword: super call must be first line of constructor 4

Special Case: Superclass Constructor Recall the special use of this in constructors? this as first line of constructor calls a different constructor for the same object E.g., UBCStudent t class Similar trick to call a superclass constructor using the super keyword: super call must be first line of constructor why? How Child Objects Are Constructed The child object contains everything the superclass object does, so the superclass constructor needs to get called. Java automatically calls the superclass s constructor, before the child s constructor starts working. What parameters does it pass? None! The special case of calling super on the first line tells Java to call the superclass constructor in the way you specify, instead of without parameters. Questions? Silly Inheritance Example Revisited We did this just to have a simple example to practice inheritance and overriding But was this a good use of inheritance? Silly Inheritance Example Revisited Silly Inheritance Example Revisited We did this just to have a simple example to practice inheritance and overriding But was this a good use of inheritance? Good: We got to re-use the name field and the getname and setname methods. We did this just to have a simple example to practice inheritance and overriding But was this a good use of inheritance? Good: We got to re-use the name field and the getname and setname methods. Bad: UBCStudents gained the undesirable property of saying random phrases. 5

Based on True Story Australian DSTO (Defence Science and Technology Organisation) military simulation. Not in Java, but in an object-oriented simulation system with the same concepts. Designers added kangaroos to the model The Real Story "For a bit of extra fun (and not for any strategic reason like kangaroos betraying your cover!) our programmers decided to put in a bit of animated wildlife It is true that the first time this was tried in the lab, we discovered that we had forgotten to remove the weapons and the 'fire' behaviour. "It is NOT true that this happened in front of a bunch of visitors (American or any other flavour). We don't normally try things for the first time in front of an audience! What I didn't relate in the talk is that since we were not at that stage interested in weapons, we had not set any weapon or projectile types, so what the kangaroos fired at us was in fact the default object for the simulation, which happened to be large multicoloured beachballs. Roughly What Happened LightInfantry +setspeed() +setstrategy() +setaaweapon() +display() Roughly What Happened LightInfantry +setspeed() +setstrategy() +setaaweapon() +display() roos[i] = new Kangaroo( ); Kangaroo roos[i].setspeed(something fast); roos[i].setstrategy(retreat); +display() Last Time: Inheritance in Java You can declare a new class as an extension of an existing one. The new class automatically inherits all the instance fields and methods of the old class. The new class can add/change fields and methods. This means the subclass objects has everything and can do everything the superclass object can. So, the subclass object is a superclass object! You can declare a new class as an extension of an existing one. The new class automatically inherits all the instance fields and methods of the old class. The new class can add/change fields and methods. This means the subclass objects has everything and can do everything the superclass object can. So, the subclass object is a superclass object! 6

This is a good test for whether one class should be a subclass of another. Do you think an object of one class is a (special kind or extension of) object of another class? If so, then this is likely a good time to use inheritance. If not, then you probably don t want to use inheritance. This is a good test for whether one class should be a subclass of another. Do you think an object of one class is a (special kind or extension of) object of another class? Are UBC students (possibly special versions of) talking dolls? This is a good test for whether one class should be a subclass of another. Do you think an object of one class is a (special kind or extension of) object of another class? Are kangaroos a specialized form of infantry soldier? What would be a good use of inheritance if we wanted to model UBCStudent, TalkingDoll, and Celebrity in a program? What s a general category that UBC students, talking dolls, and celebrities have in common? What would be a good use of inheritance if we wanted to model UBCStudent, TalkingDoll, and Celebrity in a program? What s a general category that UBC students, talking dolls, and celebrities have in common? They are all characters/personas/agents with names! Object Hierarchy So, we could create a class Person with an instance field: String name; with methods getname, setname, sayhello, A UBC student is a person, so class UBCStudent extends Person { adds methods/fields for handling library fines, etc. A celebrity is a person class Celebrity extends Person { adds methods/fields for handling big salaries A talking doll is kind of a person class TalkingDoll extends Person { adds methods/fields for saying random things 7

Object Hierarchies Suppose we also want to model cats and dogs in this program. What would be a good object hierarchy? Object Hierarchies In fact, in Java, ALL objects are actually subclasses, all descended from the class Object! (This is why every class you ve declared automatically has a tostring and clone method, although you ll usually want to override these.) Object Hierarchies Suppose we also want to model cats and dogs in this program. What would be a good object hierarchy? OK, now suppose this is a virtual world simulation, so we want a location for all these Actors Hey, we have a Point class! If we make Actor extend Point, then all of these classes will automatically have a location Good idea? Object Hierarchies OK, now suppose this is a virtual world simulation, so we want a location for all these Actors Hey, we have a Point class! If we make Actor extend Point, then all of these classes will automatically have a location Good idea? No these other objects are NOT points! The has-a Relation If you think an object has a characteristic or some information, make that an instance field in the object. Don t inherit the properties the object has. 8