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)

91 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd

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.

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.

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";

things:

Object[] Object Object

Object[] Object[]

3

IntList "Stuff"

String

int

IntList

int IntList

String ???

static type

container

dynamic type value

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[]

is a (un)wraps to

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.

Overriding and Extension

  • Notation so far is clumsy.
  • 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 defi- nition.

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.

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.

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 com- mon 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 super- class.
  • So when we write methods that operate on the superclass, they will automatically work for all subclasses with no extra work.