Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Inheriting Protected Variables  RSS feed

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator


shouldn't this be p1.A since we did not import A
 
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice observation Sandhya...
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. It makes sense now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!