Assuming that the following classes have been defined public

Assuming that the following classes have been defined: public class Dog extends Mammal { public void method1() { System.out.println(\"Dog 1\"); super.method2(); } public void method2() { System.out.println(\"Dog 2\"); } } public class Animal { public void method2() { System.out.println(\"Animal\"); } } public class Bulldog extends Dog { public void method2() { System.out.println(\"Bulldog 2\"); } } public class Mammal extends Animal { public void method3() { System.out.println(\"Mammal 3\"); method2(); } } And assuming the following variables have been defined: Mammal var1 = new Bulldog(); Animal var2 = new Animal(); Animal var3 = new Mammal(); Object var4 = new Dog(); Mammal var5 = new Dog(); Dog var6 = new Bulldog(); For each statement below, indicate the output it produces. Be sure to label each output with the numbered statement it corresponds with. If there any errors, please indicate those as well. 1) var1.method2(); 2) var2.method2(); 3) var3.method2(); 4) var4.method2(); 5) var5.method2(); 6) var6.method2(); 7) var1.method3(); 8) var2.method3(); 9) var3.method3(); 10) var4.method3(); 11) var5.method3(); 12) var6.method3(); 13) ((Bulldog)var5).method1(); 14) ((Mammal)var3).method3(); 15) ((Mammal)var4).method1(); 16) ((Bulldog)var1).method1(); 17) ((Dog)var4).method1(); 18) ((Mammal)var2).method3(); 19) ((Bulldog)var5).method1(); 20) ((Animal)var4).method2();

Solution

1) var1.method2(); -> Bulldog 2
2) var2.method2(); -> Animal
3) var3.method2(); -> Animal
4) var4.method2(); -> compilation error, class Object has not method var4.method()
5) var5.method2(); -> Dog 2
6) var6.method2(); -> Bulldog 2
7) var1.method3(); -> Mammal 3 , Bulldog 2
8) var2.method3(); -> Compilation error, Animal object has no method method3()
9) var3.method3(); -> compilation error, Mammal object has no method method3()
10) var4.method3(); -> Compilation error, class Object has not method var4.method();
11) var5.method3(); -> Mammal 3, Dog 2
12) var6.method3(); -> Mammal 3, Bulldog 2
13) ((Bulldog)var5).method1(); -> Run time error,Dog cannot be cased to Bulldog
14) ((Mammal)var3).method3(); -> Mammal 3, Animal
15) ((Mammal)var4).method1(); -> compilation error, Object has no method method1()
16) ((Bulldog)var1).method1(); -> Dog 1, Animal
17) ((Dog)var4).method1(); -> Dog 1, Animal
18) ((Mammal)var2).method3(); -> Animal cannot be casted to Mammal
19) ((Bulldog)var5).method1(); -> Dog cannot be casted to Bulldog
20) ((Animal)var4).method2(); -> Dog 2

Assuming that the following classes have been defined: public class Dog extends Mammal { public void method1() { System.out.println(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site