









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Paper; Professor: Hicks; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Fall 2002;
Typology: Papers
1 / 16
This page cannot be seen from the preview
Don't miss anything!










CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 28
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 29
CMCS 433, Fall 2002 - Michael Hicks 30
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 33
int foo
int foo
int foo
Foo int bar;
Public class Foo { int foo; static int bar; }
Class definition
Class implementation
Objects of class Foo
CMCS 433, Fall 2002 - Michael Hicks 34
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 35
CMCS 433, Fall 2002 - Michael Hicks 36
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 39
class Parent { int cost; void add (int x) { cost += x; } void add (String s) throws NumberFormatException { cost += Integer.parseInt(s); } }
CMCS 433, Fall 2002 - Michael Hicks 40
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 41
public class A { String f() {return “A.f() “; } static String g() {return “A.g() “; } } public class B extends A { String f() {return “B.f() “; } static String g() {return “B.g() “; } public static void main(String args[]) { A a = new B(); B b = new B(); System.out.println(a.f() + a.g() + b.f() + b.g()); } }
java B generates : B.f() A.g() B.f() B.g()
CMCS 433, Fall 2002 - Michael Hicks 42
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 45
CMCS 433, Fall 2002 - Michael Hicks 46
class A { String f(A x) { return “A.f(A) “; } String f(B x) { return “A.f(B) “; } static String g(A x) { return “A.g(A) “; } static String g(B x) { return “A.g(B) “; } String h = “A.h”; String getH() { return “A.getH(): ” + h; } } class B extends A { String f(A x) { return “B.f(A)/ “ + super.f(x); } String f(B x) { return “B.f(B)/ “ + super.f(x); } static String g(A x) { return “B.g(A) “; } static String g(B x) { return “B.g(B) “; } String h = “B.h”; String getH() { return “B.getH(): ” + h + “/” + super.h; } }
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 47
A a = new A(); A ab = new B(); B b = new B(); System.out.println( a.f(a) + a.f(ab) + a.f(b) ); // A.f(A) A.f(A) A.f(B) System.out.println( ab.f(a) + ab.f(ab) + ab.f(b) ); // B.f(A)/A.f(A) B.f(A)/A.f(A) B.f(B)/A.f(B) System.out.println( b.f(a) + b.f(ab) + b.f(b) ); // B.f(A)/A.f(A) B.f(A)/A.f(A) B.f(B) A.f(B) System.out.println( a.g(a) + a.g(ab) + a.g(b) ); // A.g(A) A.g(A) A.g(B) System.out.println( ab.g(a) + ab.g(ab) + ab.g(b) ); // A.g(A) A.g(A) A.g(B) System.out.println( b.g(a) + b.g(ab) + b.g(b) ); // B.g(A) B.g(A) B.g(B) System.out.println( a.h + “ “ + a.getH() ); // A.h A.getH():A.h System.out.println( ab.h + “ “ + ab.getH() ); // A.h B.getH():B.h/A.h System.out.println( b.h + “ “ + b.getH() ); // B.h B.getH():B.h/A.h
CMCS 433, Fall 2002 - Michael Hicks 48
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 51
CMCS 433, Fall 2002 - Michael Hicks 52
CMSC 433,Michael Hicks, U. Maryland
CMCS 433, Fall 2002 - Michael Hicks 53
class DumbSet { public void insert(Object o) {..} public bool member(Object o) {..}public Object any() {..} } class MyProgram { public static void main(String[] args) {DumbSet set = new DumbSet(); String s1 = “foo”; String s2 = “bar”;set.insert(s1); set.insert(s2);System.out.println(s1+”in set?”+set.member(s1)); String s = (String)set.any(); // downcast } System.out.println(“got “+s); }
CMCS 433, Fall 2002 - Michael Hicks 54
This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.