File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about packages" Watch "Question about packages" New topic

Question about packages

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
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


My Path to SCJP Certification My Path to SCWCD Certification
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

1. Yes
2. It can. The brackets are part of a static initializer (its on the exam). I think you placed code in the wrong place.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
actually B is an "instance initializer". Static initializers have to have a static modifier. But, it does the same thing at instantiation time that a static initializer does at class load time.

SCJP - 86% - June 11, 2009
Ankit Garg

Joined: Aug 03, 2008
Posts: 9313

E is correct as the method is just throwing RuntimeException(which is unchecked). It is legal for a overriding method to throw more unchecked exceptions than the overridden method...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

Well then i think there are going to be three answers correct to the question right?

B) it is initialization block
D) It is also initialization block for taste of a new object.
E) since unchecked exceptions are allowed for overridden methods

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Hi All,

Thanks for the replies.

So package p1 definitely must be imported in the Apple class? Without the import how can the current file see the file?

Ravikanth, answer D. is not incorrect as superclass protected members can only be accessed via inheritance. If it was:

{ (new Apple()).taste = "sweet"; }

it would work as you are using inheritance

B. is an instance init block. So this block runs when an instance of Apple is created e.g. via inheritance (is this right?). If this was not an instance block i.e. without brackets the code will not compile

Thing is, if I change the the reference 'taste' to public, it doesnt work as it should. I thought that if the variable is public, inherited from the superclass and package imported you could do something like

taste = "sweet";


Apple a1 = new Apple();
a1.taste = "sweeter";

Both don't compile. I understand that protected members can only be used via inheritance, but is public members should be accessed directly yes?

Please reply as im getting a little confused

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226

can anyone help with my query on accessing public members from another package?

I agree. Here's the link:
subject: Question about packages