This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

protected access modifier  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

package testpkg.p1;
public class ParentUtil {
public int x = 420;
protected int doStuff() { return x; }
}

package testpkg.p2;
import testpkg.p1.ParentUtil;
public class ChildUtil extends ParentUtil {
public static void main(String [] args) {
new ChildUtil().callStuff();
}
void callStuff() {
System.out.print("this " + this.doStuff() );
ParentUtil p = new ParentUtil();
System.out.print(" parent " + p.doStuff() );
}
}

which statement is true?

1. The code compiles and runs, with output this 420 parent 420.

2.If line 8 is removed, the code will compile and run.

3.If line 10 is removed, the code will compile and run.

4.Both lines 8 and 10 must be removed for the code to compile.

5.An exception is thrown at runtime.


The answer ios 3....Why? Can anybody explain me. I thought the answer was 1.

Thanks.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to acess a protected member of a superclass that is in another package, you must use a reference type of your class or a subclass of your class. So this was OK, because it is of type ChildUtil, but p is not OK because it is of type ParentUtil.
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike,
You said "You must use a reference of your class or a subclass of your class to access the protected member".......I am not able to understand how a subclass of the class can be used.Please can you give me an example.

Thanks
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike........I understood the concept.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ............i am clear about the concept can any one of you please explain to me.
 
Trust God, but always tether your camel... to this tiny ad.
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!