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

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: 9707
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
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic