Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Question Mock

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}
//in file B.java
package p2;
import p1.*;
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() );
}
}
It will not compile. Why??
Thank you in advance.
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What compile error message are you getting ?

Originally posted by Jordi Marqu�s:
Hi!
//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}
//in file B.java
package p2;
import p1.*;
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() );
}
}
It will not compile. Why??
Thank you in advance.


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this explanation but I dont understand very well.
Although, class B extends class A and 'i' is a protected member of A, B still cannot access i , (now this is imp) THROUGH A's reference because B is not involved in the implementation of A.
Had the process() method been defined as process(B b); b.i would have been accessible as B is involved in the implementation of B.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jordi Marqu�s:
Hi!
//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}
//in file B.java
package p2;
import p1.*;
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() );
}
}
It will not compile. Why??
Thank you in advance.



Hello Jordi.
This is a quiet thought provoking question
Good.
Now here is my thought jordi
B will inherit all the instance
members from its Super class A. However to access those inherited
members you have to use the Subclass handler ie B b to see how the modifiers work.
If you end up using the parent handler ie A a in this case
you are acutally accessing the memeber more like a member being accessed from an interface. ie "requiring public access"
Here is the funpart jordi..
if you make small change to the existing code
say like this
//Single file B.java
class A
{
protected int i = 10;
public int getI() { return i; }
}
public class B extends 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() );
}
}
It will compile just fine
So the moral of the story is
protected access modifier works
for same package,
subclass in the samepackage
and subclass is other package
Thankyou
Ragu
[This message has been edited by Ragu Sivaraman (edited September 13, 2001).]
[This message has been edited by Ragu Sivaraman (edited September 13, 2001).]
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although, class B extends class A and 'i' is a protected member of A, B still cannot access i , (now this is imp) THROUGH A's reference because B is not involved in the implementation of A.
I do not understand the above statement. I think it is wrong because variable i is protected by which it means it is accessible to all the subclasses of class A

Originally posted by Jordi Marqu�s:
I have this explanation but I dont understand very well.
Although, class B extends class A and 'i' is a protected member of A, B still cannot access i , (now this is imp) THROUGH A's reference because B is not involved in the implementation of A.
Had the process() method been defined as process(B b); b.i would have been accessible as B is involved in the implementation of B.


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roopa,
'protected' is a strange beast JLS §6.6.2 states:


6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed
from outside the package in which it is declared only by code
that is responsible for the implementation of that object.


Which basically means that the subclass can only access the protected members of the superclass if it actually creates and uses the superclass member. You can't just pass a superclass object to one of it's methods.
Try adding a field in B, "myA a = new A()", and then printing "myA.i".
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Actually i did not try this out but if in the above
code i make the following change in subclass method process
as follows . Will the code work ?


public void process(A a)
{
a = new A();
a.i = a.i*2;
}



[This message has been edited by Angela Narain (edited September 14, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic