CS61B Lecture #8: Object-Oriented Mechanisms

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

CSE 431S Type Checking. Washington University Spring 2013

Java Object Oriented Design. CSC207 Fall 2014

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

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Introduction to Programming Using Java (98-388)

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

VARIABLES AND TYPES CITS1001

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

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

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

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

Introduction to Inheritance

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

COMP 250 Fall inheritance Nov. 17, 2017

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

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

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

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

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

Chapter 5 Object-Oriented Programming

CS260 Intro to Java & Android 03.Java Language Basics

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

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

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

Points To Remember for SCJP

Rules and syntax for inheritance. The boring stuff

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

CLASS DESIGN. Objectives MODULE 4

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CS61B Lecture #13: Packages, Access, Etc.

Inheritance (Part 5) Odds and ends

CS-202 Introduction to Object Oriented Programming

index.pdf January 21,

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

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

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

A Short Summary of Javali

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

PIC 20A Number, Autoboxing, and Unboxing

CS61B Lecture #13: Packages, Access, Etc.

ITI Introduction to Computing II

Basic Object-Oriented Concepts. 5-Oct-17

ITI Introduction to Computing II

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

CS 2110 AEW SIGN UP FOR 1 CREDIT S/U COURSE!!!

CS 2110 AEW. Machine Language. Outline. Assembly Language. High-Level Language 1/26/2012

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

ECE 122. Engineering Problem Solving with Java

Index COPYRIGHTED MATERIAL

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

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

Lesson 3: Accepting User Input and Using Different Methods for Output

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Lecture 10. Overriding & Casting About

CS61B Lecture #9: Interfaces and Abstract Classes

Java and C CSE 351 Spring

2. The object-oriented paradigm

. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

TeenCoder : Java Programming (ISBN )

CS61B Lecture #12. Public Service Announcement. Miscellaneous Topics: What to do About Errors? throw new SomeException (optional description);

Overriding methods. Changing what we have inherited from e.g. Object

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

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Java Fundamentals (II)

Lecture #23: Conversion and Type Inference

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

The Pyth Language. Administrivia

Java. Representing Data. Representing data. Primitive data types

Declarations and Access Control SCJP tips

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

The class Object. Lecture CS1122 Summer 2008

Fall 2017 CISC124 9/16/2017

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

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Overriding Variables: Shadowing

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

IC Language Specification

Programming overview

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Introduction to Object-Oriented Programming

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

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance -- Introduction

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

CSE 307: Principles of Programming Languages

Chapter 2: Using Data

CSCI 355 Lab #2 Spring 2007

15CS45 : OBJECT ORIENTED CONCEPTS

Object Oriented Programming: Based on slides from Skrien Chapter 2

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Transcription:

CS61B Lecture #8: Object-Oriented Mechanisms Public Service Announcement: CalSol, the UC Berkeley solar car team, is looking for new members. Preferably in MechE and EECS, but all welcome. See their web site, www.me.berkeley.edu/calsol, for more. Readings: Chapter 2, Values, Types, and Containers, in Assorted Notes on Java (in the reader). Chapters 7 and 8 in Head First Java. Today: New in this lecture: the bare mechanics of object-oriented programming. The general topic is: Writing software that operates on many kinds of data. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 1

Overloading Problem: How to get System.out.print(x) or stdout.put(x) to print x, regardless of type of x? In Scheme, one function can take an argument of any type, and then test the type. In Java, methods specify a single type of argument. Partial solution: overloading multiple method definitions with the same name and different numbers or types of arguments. E.g., System.out has type java.io.printstream, which defines void println() Prints new line. void println(string s) Prints S. void println(boolean b) Prints "true" or "false" void println(char c) Prints single character void println(int i) Prints I in decimal etc. Each of these is a different function. Compiler decides which to call on the basis of arguments types. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 2

Generic Data Structures Problem: How to get a list of anything or array of anything? Again, no problem in Scheme. But in Java, lists (such as IntList) and arrays have a single type of element. First, the short answer: any reference value can be converted to type java.lang.object and back, so can use Object as the generic (reference) type : Object[] things = new Object[2]; things[0] = new IntList (3, null); things[1] = "Stuff"; // Now ((IntList) things[0]).head == 3; // and ((String) things[1]).startswith("st") is true // things[0].head Illegal // things[1].startswith ("St") Illegal Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 3

Dynamic vs. Static Types Every value has a type its dynamic type. Every container (variable, component, parameter), literal, function call, and operator expression (e.g. x+y) has a type its static type. Therefore, every expression has a static type. Object[] things = new Object[2]; things[0] = new IntList (3, null); things[1] = "Stuff"; Object[] Object Object things: Object[] Object[] IntList String static type container dynamic type value IntList int 3 <nulltype> "Stuff"??? String int IntList Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 4

Type Hierarchies A container with (static) type T may contain a certain value only if that value is a T that is, if the (dynamic) type of the value is a subtype of T. Likewise, a function with return type T may return only values that are subtypes of T. All types are subtypes of themselves(& that s all for primitive types) Reference types form a type hierarchy; some are subtypes of others. null s type is a subtype of all reference types. All reference types are subtypes of Object. int double boolean... Object is a (un)wraps to Integer Double Boolean String IntList int[] Object[]... String[] <nulltype> Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 5

The Basic Static Type Rule Java is designed so that any expression of (static) type T always yields a value that is a T. Static types are known to the compiler, because you declare them, as in String x; // Static type of field int f (Object s) { // Static type of call to f, and of parameter int y; // Static type of local variable or they are pre-declared by the language (like 3). Compiler insists that in an assignment, L = E, or function call, f(e), where void f (SomeType L) {..., E s static type must be subtype of L s static type. Similar rules apply to E[i] (static type of E must be an array) and other built-in operations. Slight fudge: compiler will coerce smaller integer types to larger ones, float to double, and (from last lecture) between primitive types and their wrapper types. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 6

Consequences of Compiler s Sanity Checks This is a conservative rule. The last line of the following, which you might think is perfectly sensible, is illegal: int[] A = new int[2]; Object x = A; // All references are Objects A[i] = 0; // Static type of A is array... x[i+1] = 1; // But not of x: ERROR Compiler figures that not every Object is an array. Q: Don t we know that x contains array value!? A: Yes, but still must tell the compiler, like this: ((int[]) x)[i+1] = 1; Defn: Static type of cast (T) E is T. Q: What if x isn t an array value, or is null? A: For that we have runtime errors exceptions. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 7

Notation so far is clumsy. Overriding and Extension Q: If I know Object variable x contains a String, why can t I write, x.startswith("this")? A: startswith is only defined on Strings, not on all Objects, so the compiler isn t sure it makes sense, unless you cast. But, if an operation were defined on all Objects, then you wouldn t need clumsy casting. Example:.toString() is defined on all Objects. You can always say x.tostring() if x has a reference type. The default.tostring() function is not very useful; on an IntList, would produce string like "IntList@2f6684" But for any subtype of Object, you may override the default definition. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 8

Overriding tostring For example, if s is a String, s.tostring() is the identity function (fortunately). For any type you define, you may supply your own definition, as we did in class IntList: public String tostring () { StringBuffer b = new StringBuffer (); b.append ("["); for (IntList L = this; L!= null; L = L.tail) b.append (" " + L.head); b.append ("]"); return b.tostring (); Ifx = new IntList (3, new IntList (4, null)),thenx.tostring() is "[3 4]". Conveniently, the"+" operator on Strings calls.tostring when asked to append an Object, and so does the "%s" formatter for printf. With this trick, you can supply an output function for any type you define. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 9

Extending a Class To say that class B is a direct subtype of class A (or A is a direct superclass of B), write class B extends A {... By default, class... extends java.lang.object. The subtype inherits all fields and methods of its superclass (and passes them along to any of its subtypes). InclassB,youmayoverride aninstancemethod(not astaticmethod), by providing a new definition with same signature (name, return type, argument types). I ll say that a method and all its overridings form a dynamic method set. The Point: If f(...) is an instance method, then the call x.f(...) calls whatever overriding of f applies to the dynamic type of x, regardless of the static type of x. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 10

class Worker { void work () { collectpay (); class Prof extends Worker { // Inherits work () Illustration class TA extends Worker { void work () { while (true) { dolab(); discuss(); officehour(); Prof paul = new Prof (); paul.work() ==> collectpay(); TA mike = new TA (); mike.work() ==> dolab(); discuss();... Worker wpaul = paul, wpaul.work() ==> collectpay(); wmike = mike; wmike.work() ==> dolab(); discuss();... Lesson: For instance methods (only), select method based on dynamic type. Simple to state, but we ll see it has profound consequences. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 11

What About Fields and Static Methods? class Parent { int x = 0; static int y = 1; static void f() { System.out.printf ("Ahem!%n"); static int f(int x) { return x+1; class Child extends Parent { String x = "no"; static String y = "way"; static void f() { System.out.printf ("I wanna!%n"); Child tom = new Child (); tom.x ==> no ptom.x ==> 0 Parent ptom = tom; tom.y ==> way ptom.y ==> 1 tom.f() ==> I wanna! ptom.f() ==> Ahem! tom.f(1) ==> 2 ptom.f(1) ==> 2 Lesson: Fields hide inherited fields of same name; static methods hide methods of the same signature. Real Lesson: Hiding causes confusion; so understand it, but don t do it! Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 12

What s the Point? The mechanism described here allows us to define a kind of generic method. A superclass can define a set of operations (methods) that are common to many different classes. Subclasses can then provide different implementations of these common methods, each specialized in some way. All subclasses will have at least the methods listed by the superclass. So when we write methods that operate on the superclass, they will automatically work for all subclasses with no extra work. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 13