Wednesday, June 6, 2007

Final exam review



Final exam review:

-----
"Immutable"
How to make a class immutable?
What is a sample class?

-----
Override vs. Overload?

Consider the following program. Which overrides which? Which overloads which?

public class A {
public void f() {
System.out.println("1");
}

public void g(int x) {
System.out.println("2");
}
}

public class B extends A {
private void f() {
System.out.println("3");
super.f();
}

public void f(int x) {
System.out.println("4");
}

public void g() {
System.out.println("5");
}

// can we have this method?
private void g(int x) {
System.out.println("6");
}
}

Consider the following calls. Which method is called at each line? Which line is an error?

A a = new A();
B b = new B();
a.f(5);
b.f();
b.g(2);
((A)b).f();
A.f();

Answers:
a.f(5); => not compiled
b.f(); => 3, 1
b.g(2); => 2
((A)b).f(); => 1
A.f(); => not compiled

-----
What is "this"?

public class C {
private void f() {
if (this.equals(null)) {
System.out.println("Duh?");
}
}

public static void main(String args) {
f();
OR this.f();
OR (new C).f();
}

}
-----
interface vs abstract class
What are the differences?
When to use which?
-----

No comments: