2. (12 points) Suppose we have the following Java classes: public class Foo priv
ID: 3805270 • Letter: 2
Question
2. (12 points) Suppose we have the following Java classes: public class Foo private String my public Foo (String m) this .mystring m; public int length f return this .mystring. length (1; public class Bar extends Foo private Stri str; public Bar (String s1 String s2) super (s1); this str s2 public int length return this. str.length public String gee tstr return this str public class FooUtils public void fooCheck (Foo f System. out.println ("It's a Foo!") public void fooCheck (Bar b) System. out.println ("It's a Bar +b.getStrExplanation / Answer
b)
clearly : length() method in overrided in Bar class
all overrided methods are run time polymorphism(dynamic polymorphism)
So , it calls Bar's length method.
Output: 4
c).
fooCheck() : method are overloaded. One take Bar's class object and second take Foo's class object
overloading is the static polymorphism (compile time polymorphism).
So all when we call oveloaded method, ot only check reference type, not actiual object.
FooUtils.fooCheck(b);
// this will call: fooCheck(Bar b) method. because 'b' is the object of Bar class.
// Output: It is a Bar!test
FooUtils.fooCheckfb);
// this will call: fooCheck(Foo f) method. because 'f' is the object of Foo class.
// Output: It is a Foo!
d)
Foo f = new Foo("Hello world!");
Bar b = (Bar) f;
We can not typecae super class Object in Child class object.
Child class reference can not hold Super class objects
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.