CSE143 Inheritance Example handout #15
Assuming that the following classes have been defined:
public class One {
public void method1() {
System.out.println("One1");
}
}
public class Two extends One {
public void method3() {
System.out.println("Two3");
}
}
public class Three extends One {
public void method2() {
System.out.println("Three2");
method1();
}
}
public class Four extends Three {
public void method1() {
System.out.println("Four1");
super.method1();
}
public void method3() {
System.out.println("Four3");
}
}
And assuming the following variables have been defined:
One var1 = new Two();
One var2 = new Three();
One var3 = new Four();
Three var4 = new Four();
Object var5 = new Three();
Object var6 = new One();
In the table below, indicate in the right-hand column the output produced by
the statement in the left-hand column. If the statement produces more than one
line of output, indicate the line breaks with slashes as in "a/b/c" to indicate
three lines of output with "a" followed by "b" followed by "c". If the
statement causes an error, fill in the right-hand column with either the phrase
"compiler error" or "runtime error" to indicate when the error would be
detected.
Statement Output
------------------------------------------------------------
var1.method1(); ____________________________
var2.method1(); ____________________________
var3.method1(); ____________________________
var4.method1(); ____________________________
var5.method1(); ____________________________
var6.method1(); ____________________________
var4.method2(); ____________________________
var4.method3(); ____________________________
((Two)var1).method2(); ____________________________
((Three)var1).method2(); ____________________________
((Two)var1).method3(); ____________________________
((Four)var2).method1(); ____________________________
((Four)var3).method1(); ____________________________
((Four)var4).method3(); ____________________________
((One)var5).method1(); ____________________________
((Four)var5).method2(); ____________________________
((Three)var5).method2(); ____________________________
((One)var6).method1(); ____________________________
((One)var6).method2(); ____________________________
((Two)var6).method3(); ____________________________
Solution to CSE143 Inheritance Example
1. Inheritance Mystery. The output is as follows.
Statement Output
--------------------------------------------------
var1.method1(); One1
var2.method1(); One1
var3.method1(); Four1/One1
var4.method1(); Four1/One1
var5.method1(); compiler error
var6.method1(); compiler error
var4.method2(); Three2/Four1/One1
var4.method3(); compiler error
((Two)var1).method2(); compiler error
((Three)var1).method2(); runtime error
((Two)var1).method3(); Two3
((Four)var2).method1(); runtime error
((Four)var3).method1(); Four1/One1
((Four)var4).method3(); Four3
((One)var5).method1(); One1
((Four)var5).method2(); runtime error
((Three)var5).method2(); Three2/One1
((One)var6).method1(); One1
((One)var6).method2(); compiler error
((Two)var6).method3(); runtime error