



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




33
35
int i = 6; int j; // uninitialized int [] a = {1, 3, 5, 7, 9}; int [] b = new int[3]; String s = “abcdef”; String t = null; 37
j = i; b = a; t = s;
38
a[1] = 0; // also changes b[1]
o.m(arg1, arg2, ..., argn);
class Parent { int cost; void add(int x) { cost += x; } } class Child extends Parent { void add(int x) { if (x > 0) cost += x; } } 43
50
class Parent { int cost; void add(int x) { }^ cost += x; void add(Object s) throws NumberFormatException { cost += Integer.parseInt((String)s); } } class Child extends Parent { void add(String s) throws NumberFormatException { if (x > 0) cost += Integer.parseInt(s); } } Child c = new Child(); c.add((Object)“-1”); System.out.println(c.cost); Prints - 51
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 53
54 Actual type B Declared type A
A a = new B();
public class A { static String g() { return “This is A! “; } } public class B extends A { static String g() { return “This is B! “; } public static void main(String args[]) { A a = new B(); B b = new B(); System.out.println(a.g() + b.g()); } }
56
public class A { static String g() { return “This is A! “; } } public class B extends A { static String g() { return “This is B! “; } public static void main(String args[]) { System.out.println(A.g() + B.g()); } }
57
61
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); } }