• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Inheriting Protected Variables

 
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, this is a SCJP1.5 tip from Devesh Chanchlani's site:

http://devesh2k1.googlepages.com/home

The following wont compile:

package p1;
public class A {
protected int i = 10;
public int getI() { return i; }
}

package p2;
public class B extends p1.A {
public void process(A a)
{ a.i = a.i*2; }
public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}


The reason given is that the variable 'i' is protected and B cannot access it. If the process method was defined as void process(B b){ b.i....etc then it will work.

My questions are:

1. Where will this error occur? On the creation of the method process (A a) or when it it called in the main method - b.process(a); ?
2. Can protected variables only be accessed in a subclass in another package using the instance of the class (i.e. B)?
3. Is Qs 2 the same for protected methods?


Thanks
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Santiago Bravo:

My questions are:

1. Where will this error occur? On the creation of the method process (A a) or when it it called in the main method - b.process(a); ?
2. Can protected variables only be accessed in a subclass in another package using the instance of the class (i.e. B)?
3. Is Qs 2 the same for protected methods?


Thanks



1) EDIT: I answered your first question too quickly... i.e., I should of read it before my fingers did their typing... the error will be here:
{ a.i = a.i*2; } my apologies... i realized my error in the first mile of my after work run.

2) Protected members are accessible as either through inheritence or being part of the same package.

3) Yes
[ October 08, 2008: Message edited by: Paul Campbell ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


shouldn't this be p1.A since we did not import A
 
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
Nice observation Sandhya...
 
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
Thanks Paul. It makes sense now
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic