Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown


Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • 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

valan vel
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • 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.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic