READ THE CODE BELOW AND WRITE DOWN PRINTED VALUES FOR EACH LINE: public interfac
ID: 3729142 • Letter: R
Question
READ THE CODE BELOW AND WRITE DOWN PRINTED VALUES FOR EACH LINE:
public interface Doable public abstract void doit (); public class Thing2 implements Doable public class Thingi implements Doable private static final String SPEAK - "Me"; public static final String SPEAK - "No, Me" : public Thing10 public Thing2 (0 // ctor initialization here // ctor initialization here public String speak () public String speak ( Strings) return SPEAK; return SPEAK + 8; public void doit () public void doit (0) // Thing2 does its thing // Thingi does its thing And the following variable definitions: Thing1 thing1; Thing2 thing2; Doable doable; Indicate which are valid Java statements. Consider each statement executed sequentially in the order it appears. A) Valid Java statement – No Compiler Error B) Invalid Java statement – Compiler Error Hint: What does the compiler know about any reference variable at compile time (vs. run time)? thing1 - new Thingi(); String s2 - Thing2.SPEAK; thingl.speak (); doable = new Thing1 (0 : thing1.doit (); doable.speak (): thingl. speak ("Mine" ); doable.doit(); String sl = Thingl.SPEAK; doable = thing2; thing2 - new Thing2 (0 : doable.speak ( " Mine" ); thing2.speak(); doable.doit(); thing2.doit (); thingl - thing2; thing2.speak ("Mine" ); thingl = doable; doable = new Doable();Explanation / Answer
things1 = new Things1(); - Valid
things1.speak(); Valid
things1.doit(); Valid
things1.speak("Mine"); Invalid because Things1 class have speak() without any signature or parameter
String s1=Things1.SPEAK; Invalid, because if you call like this through other class then SPEAK variable in Things1 class in invisible i.e private and cannot be accessible but in the Things1 class if you call like this it works because private instance can be call in its own class.
things2=new Things2(); Valid
things2.speak(); Invalid, beacuse in Things2 class speak() is parameterized or you can say that there is one parameter in speak method i.e speak(String param), so without parameter speak() cannot be call through Things2 class.
things2.doit(); Valid
things2.speak("Mine"); Valid
String s2=Things2.SPEAK; Valid, because in this SPEAK variable is public and accessible for every one
doable=new Things1();Valid
doable.speak(); Invalid, there no speak method declare in Doable interface.
doable.doit(); Valid
doable=things2; valid
doable.speak("Mine"); invalid
doable.doit(); valid
things1=things2; Invalid, Type mismatch it is, i.e cannot convert from things2 to things1
things1=doable; Invalid Type mismatch
doable= new Doable(); Invalid, we cannot instantiate the type Doable or you can say that we cannot create object of Interface.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.