Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS61B Lecture #8: Object-Oriented Mechanisms - Java Programming, Slides of Data Structures and Algorithms

A part of the cs61b lecture notes on object-oriented mechanisms in java. It covers topics such as overloading, generic data structures, dynamic vs. Static types, type hierarchies, the basic static type rule, consequences of the compiler's 'sanity checks', overriding and extension, and illustrations. The lecture also includes examples and explanations of various concepts.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

netii
netii 🇮🇳

4.4

(7)

97 documents

1 / 13

Toggle sidebar

Related documents


Partial preview of the text

Download CS61B Lecture #8: Object-Oriented Mechanisms - Java Programming and more Slides Data Structures and Algorithms in PDF only on Docsity! 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 pro- gramming.” • 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 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 oth- ers. null’s type is a subtype of all reference types. • All reference types are subtypes of Object. int double boolean ... Object Integer Double Boolean String IntList int[] Object[] ... String[] <nulltype> is a (un)wraps to 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 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). • In class B, you may override an instance method (not a static method), 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, re- gardless of the static type of x. Last modified: Fri Feb 3 13:19:05 2006 CS61B: Lecture #8 10 Illustration class Worker { void work () { collectPay (); } } class Prof extends Worker { // Inherits work () } 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
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved