Oop 4
Oop 4
Ham
Ham a
Ham b
Ham
Ham a
Lamb b
public class Ham {
int a = 0;
ÏϧÏHam 0 1
int b = 1; ÏϧÏSpam 2
public void a() {
System.out.println("Ham “ + a);
ÏϧÏHam1
} ÏϧÏ0
public void b() {
System.out.println("Ham “ + b); ÏϧÏ1
} ÏϧÏ
public String toString() {
return "Ham “ + a + “ “ + b; ÏϧÏHam 0 1
} ÏϧÏYam 2
}
ÏϧÏYam 3
public class Spam extends Ham { ÏϧÏ0
int a = 2;
public void a() { ÏϧÏ1
}
System.out.println("Spam “ +a); ÏϧÏ
} ÏϧÏHam 0 1
public class Yam extends Spam {
ÏϧÏHam0
int b = 3; ÏϧÏHam1
public void a() {
System.out.println("Yam “ + a); ÏϧÏ0
} ÏϧÏ1
public void b() {
System.out.println(“Yam “ + b);
}
}
System.out.println(food[i].a);
System.out.println(food[i].b);
System.out.println();
}
}
}
public class A public class C extends A
{ {
private String x = "Ax"; private String x = "Cx";
protected String y = "Ay";
public String z = "Az"; public static void main(
String [] args)
public String toString() { {
return x + y + z; C c = new C();
} System.out.println(c.x);
System.out.println(c);
public static void main( }
String [] args) }
{
A a = new A(); public class D extends C
System.out.println(a); {
} private String x = "Dx";
} public String z = "Dz";
The next line prints the return value of D's toString method. The toString method is defined
locally to override the inherited one (unlike in the example for class C, where the toString
method is inherited instead of overridden). Because the toString method is overridden, when it
refers to x, y, and z, it refers to the variables inside of class D. So this returns "DxAyDz".
Compare this to the inherited toString method in class C, which returns "AxAyAz".
The next two lines use a variable with static type C to refer to an object of dynamic type D.
Notice that it is an error to try to print (or refer to) c.x. That's because 'x' is private in class C,
and this code is written inside class D. Also notice that c.z is "Az", whereas d.z is "D.z". For
fields, Java uses the value of the static type's field (in this case, the value of z from class C,
which is inherited from class A and has value "Az").
The last line prints the value of c.toString(). Java uses the value of a the static type's field, but
the dynamic type's methods. Variable c has dynamic type D, because it refers to an object of
type D. So Java uses the toString method defined in class D, which returns the values of x, y,
and z within class D (or "DxAyDz"). Notice the difference between how fields get handled, and
how methods get handled. The field c.z refers to the field defined in class C (which is inherited
from class A). The method c.toString() refers to the method defined in class D, not class C.
I have still not figured out any reason why Java does shadow things this way for fields. It's very
confusing, and it can lead to very hard-to-fix bugs. In general, it is HIGHLY RECOMMENDED
that you AVOID defining fields with the same name as a superclass's field. Sometimes though
(like when you're extending a superclass from the Java API), you may not know what the
superclass's fields are called, and in that case, you just have to guess.
2. Program Development
Here's a problem from a previous Final Exam.
Consider the following skeleton for a Robot class, which has private fields for storing the
location of aRobot object, its name, and the direction it’s facing (North for a direction parallel to
the positive y axis,South for the negative y axis, East for the positive x axis, or West for the
negative x axis). It also hasstub methods for constructing a Robot object, changing the direction,
and moving the location of therobot in the direction it’s facing.
// turn 90 degrees clockwise, e.g. ’N’ changes to ’E’, ’E’ to ’S’, ...
public void turnClockwise() { ... }
(a)Assuming the class above is completed correctly, what does the following program display
on the screen:
robby.takeSteps(3);
System.out.println(robby);
}
}
Displayed on screen:
(b) Complete the constructor, the turnClockwisemethod, and the takeStepsmethod. Make sure
your constructor validates its input. You do not need to define turnCounterClockwise.
(c) Write Java code to create an array of 5 robots. Use a for loop to fill in the array so that the n-th
robot is named “robot n”, and it starts off life facing east at location (n, n).
Here's another problem from a previous Final Exam. This one is an inheritance/polymorphism
question.
classSuperClass
{
protectedint x = 0;
publicSuperClass(int x)
{
this.x = x;
}
sc = new SubClass(3);
sc.display();
}
}
(b)List the name of all methods that are visible in subclasses of SuperClass (in other words,
methodsthat can be called directly).
add can be called directly just be using the name add(). the constructor SuperClass can be
called by using the super() constructor. the display() method from SuperClass is overridden by
the display() method in the SubClass, but it can still be called by writing super.display(). In
summary, any method that has public or protected access in the superclass can be called directly
by the subclass.
(c)List the name of all methods that may NOT be overridden by any subclasses of SuperClass.
methods that are declared to be final in the superclass may not be overridden. So the add()
method may not be overridden.
displayed on screen:
3
5