• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question about packages

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well then i think there are going to be three answers correct to the question right?

since
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
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Apple.java see the Fruit.java 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";

or

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

thanks
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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


Thanks
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic