Overriding Method Exerciser
Below is a class PrintClass and its subclass PrintSubClass.
Edit PrintSubClass so that it overrides PrintClass's printMe()
method so that it not only prints out the value of x and y
but also z. Run the program and be able to explain how it works.
class PrintClass {
int x = 0;
int y = 1;
void printMe() {
System.out.println("x is " + x + ", y is " + y);
System.out.println("I am an instance of the class " +
this.getClass().getName());
}
}
class PrintSubClass extends PrintClass {
int z = 3;
public static void main(String arguments[]) {
PrintSubClass obj = new PrintSubClass();
obj.printMe();
}
}
Solution |