Hi All,
I have a query about a question from Whizlabs:
-------------------------------------------------------------------------------------------
Given the following:
package p1;
public class Fruit{
protected
String taste;
protected void changeColor(){}
}
package p2;
public class Apple extends Fruit{
//LINE 1
}
What can be inserted at LINE 1 - choose 2 options:
A. protected int changeColor{}
B. { taste = "sweet"; }
C. void changeColor{}
D. { (new
Fruit()).taste = "sweet"; }
E. protected void changeColor{} throws RuntimeException
-------------------------------------------------------------------------------------------
From the above, A. and C. are illegal overrides. E. is a legal override. However that leaves B. and D. D is incorrect as to access the superclass
members you need to use inheritance.
I am unsure about B. as I thought the package p1 had to be imported in the Apple class?
Whizlabs gave the correct answer as B. and D.
When I try to code the above, I get a 'cannot find symbol class Fruit'. If I import package p1 in the Apple class I get no errors if I insert the code for B. or E.
My queries are: 1. Does the package need to be imported in the subclass and 2. if B. didnt have the brackets {} why can't the superclass members be accessed?
e.g. if B. was only: taste = "sweet"; I get a compiler error
Thanks