• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

confused

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ranchers
can any one give a detailed explaination
on this question

1. package testpkg.p1;
2. public class ParentUtil {
3. public int x = 420;
4. protected int doStuff() { return x; }
5. }
1. package testpkg.p2;
2. import testpkg.p1.ParentUtil;
3. public class ChildUtil extends ParentUtil {
4. public static void main(String [] args) {
5. new ChildUtil().callStuff();
6. }
7. void callStuff() {
8. System.out.print("this " + this.doStuff() );
9. ParentUtil p = new ParentUtil();
10. System.out.print(" parent " + p.doStuff() );
11. }
12. }

which statement is true?
A. The code compiles and runs, with output this 420 parent 420.
B. If line 8 is removed, the code will compile and run.
C. If line 10 is removed, the code will compile and run.
D. Both lines 8 and 10 must be removed for the code to compile.
E. An exception is thrown at runtime.


ans is C

by
valan vel
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi here,
superclass, subclass are in two different packages.
dostuff() method of super class is having 'protected' access.

A subclass outside the package can inherit the protected memeber.
A subclass in different package can access the protected member only through inheritance.
A subclass outside the package can't use a superclass reference to access a protected member.

In the given program, subclass method classStuff() is tryingto access the superclass protected member using the superclass reference. It will be compilation error. If we comment that line program will compile and run.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic