As discussed in class, if we removed the call to super(s1) in the constructor fo
ID: 3805093 • Letter: A
Question
As discussed in class, if we removed the call to super(s1) in the constructor for Bar, this code would not compile. Explain in 1-3 short sentences what the call to super(s1) does in the constructor for Bar and why it is necessary here.
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.getstrExplanation / Answer
When the Parent class has no argument constructor the complier will add super() and will call the parent.
If we don't explicitly call a super constructor the argument less constructor (super()) will be called automatically
However when we have Parent class has parameterised constructor then its mandatory to call super with arguments otherwise we will get complie time error. Super will initalise the value passed i.e it will call the parent class constructor with argument passed and will intialise the value.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.