Question: c. Suppose a program contains the following lines of code: Bar b = new Bar(This is a,test); Foo f = (Foo) b; FooUtils.fooCheck(b); FooUtils.fooCheck(f); The
c. Suppose a program contains the following lines of code: Bar b = new Bar("This is a","test"); Foo f = (Foo) b; FooUtils.fooCheck(b); FooUtils.fooCheck(f); The calls FooUtils.fooCheck(b); and FooUtils.barCheck(f) use static dispatch as discussed in class. Explain in 2-5 short sentences which fooCheck() method will be executed for each of the calls and why. You may (and probably should) plug this code into a program and execute it to see what behavior it has, but your explanation must explain why static dispatch causes it to behave this way. d. Suppose a program contains the following lines of code: Foo f = new Foo("Hello world!"); Bar b = (Bar) f; FooUtils.fooCheck(b); FooUtils.fooCheck(f); If you attempt to do this, Java will throw a ClassCastException at runtime on the line Bar b = (Bar) f. The code in part (c) runs without an exception, so why will this not work? Explain in 2-5 short sentences why this code cannot work. Your answer must discuss polymorphism and inheritance.
2. (12 points) Suppose we have the following Java classes: public class Foo private string mystring; public Foo (String m) this .mystring m; public int length o return this .mystring. length public class Bar extends Foo private String str public Bar (String sl String 52) super (s1) this str s2 i public int length 1 return this. str. ho, length public String getstr o return this. str; public class F ooUtils public void foocheck (Foo f) System.out.println ("It's a Foo h public void foocheck Bar b) System.out.println ("It's a Bar b.getstrStep by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
