aspose file tools*
The moose likes Beginning Java and the fly likes Please explain this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please explain this code" Watch "Please explain this code" New topic
Author

Please explain this code

Sajan Joseph
Ranch Hand

Joined: Jan 05, 2001
Posts: 40
I have been doing the exercises in Thinking In Java and came across this one. Can anyone please explain this to me :
package p1;
public class A{// A is in package p1
protected void a(){
System.out.println("Hello");
}
}
package p2;
import p1.*;
public class B extends A{ // B is a sub class of A in another
// package p2

public static void main(String args[]){
A a1=new A();
a1.a();
}
}
The compiler gives me an error message : a() has protected access in class A.
Changing the code in class B to:
A a1=new B() ---> also does not help. Compiler gives the same error message.
The code compiled and ran only when I changed the code :
B a1=new B();
My question is :
As per the defenition of protected, another package subclass can invoke a protected method in its superclass. B is a subclass of A in package p2. Why is it not able to access a() when I had written :
A a1=new A();
a1.a();
OR when I had written :
A a1=new B(); // trying late binding (dynamic method invocation)
a1.a();

Can anybody clarify this to me.
Thank you,
SJ
Michael Mendelson
Ranch Hand

Joined: Dec 19, 2000
Posts: 73
Sajan, this code worked for me...

I removed the package references for simplicity. I suggest deleting all A.class and B.class files and trying again.
Or, for simplicity's sake, try compling them from the same file.

[This message has been edited by Michael Mendelson (edited January 13, 2001).]
Sajan Joseph
Ranch Hand

Joined: Jan 05, 2001
Posts: 40
Originally posted by Michael Mendelson:
[B]Sajan, this code worked for me...

I removed the package references for simplicity. I suggest deleting all A.class and B.class files and trying again.
Or, for simplicity's sake, try compling them from the same file.

[This message has been edited by Michael Mendelson (edited January 13, 2001).][/B]

Michael,
I guess for protected access modifier to be tested, you need to put both classes in different packages. What I am not able to understand is why I cannot instantiate an object of class A in class B, and call the protected method a() on the object of A.
Can you clarify that to me .
SJ
Daniel Olson
Ranch Hand

Joined: Jan 05, 2001
Posts: 81
After reading your question, I tried writing class B without the import statement.

If you do this, the compiler will not recogize the A constructor.

So I wonder if the reason the compiler complains "a() has protected access in class A" is that A is coming from the import and considered an outside class, while B is a subclass which can access the protected method.
I like to see an explanation from someone who understands this better.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Sajan,
From RHE 2nd Ed regarding the use of the protected access modifier:

A protected feature of a class is available to all classes in the same package, just like a default feature. Moreover, a protected feature of a class is available to all subclasses of the class that owns the protected feature.

Okay, to your example. I too get the same compiler error when the classes are in different package.

You're trying to access a() outside package p1. This is not allowed, because a() is a protected. Granted that the handle a1 is to the class A which technically owns the method. If you try to acess the method outside the package, the compiler will not allow it.

The reason why the above code works is from the second sentence in RHE. Now you're invoking a() via a B reference. Since B is a subclass of A and has all access to A's public and protected method, you can invoke the method via the clas B reference handle.
That's my interpretation. I'm curious what Michael has to say, since I'm quoting his book. His example works because both classes are in the same package.
-Peter
Sajan Joseph
Ranch Hand

Joined: Jan 05, 2001
Posts: 40
Thanks Peter, Daniel Michael,
I am still not sure I have understood this properly. What I have understood is that a protected member can be accessed in a different package subclass only through the sub class reference, not through the superclass reference.
I do not have any actual coding experience. Is this feature used frequently in programming? What advantage does a class developer get in declaring a member as protected? And how does it affect the client who uses it?
SJ
hari pillai
Greenhorn

Joined: Jan 15, 2001
Posts: 3
Hi Sajan,
I think the fact that you cannot access the protected super classmethod/field from a subclass in other package is purposely implemented to make the method/field ,well,protected from classes in other packages.
Whoever makes a super class method protected thereby allows only subclasses in the same package to use its methods or manipulate itsfields. Users in other package gets the method/field of the subclass to play with .
This is my belief which is always open for correction.
Swati Samudra
Greenhorn

Joined: Dec 27, 2000
Posts: 16
If I save class A in package P1 and class B in default package that is current directory. I am able to compile and run the program. Even if I give A a1=new A(); or B a1=new B(). I am not having any problem with this.
But I have another problem. If I save class A in package P1 and class B in another package P2. I am able to compile A.java by keeping my current directory P1. But I am not able to compile B.java. Because if I compile from P2 directory I am getting the message "package P1 does not exist" and if I compile from one-up directory I get "can't read B.java".
How can I compile B.java which is in another package, importing class from A.java which is another package.
I don't know how to do this. My problem is I am not able to compile file in different package which is importing classes from another package. From which prompt should I compile the file B.java.
Swati Samudra
Greenhorn

Joined: Dec 27, 2000
Posts: 16
Sajan,
I have found the solution to my problem. My classpath was not set to one-up, but one above the one-up in Autoexec.bat, so I was having the problem in compiling B.java. While compiling A.java I was making P1 as my current directory. But while compiling B.java if I made P2 as my current directory it would not able to find P1 package. So I had to set classpath to one-up above the package in my Autoexec.bat file and I was able to compile both the files.
Let me inform you, the program is just fine and runs with any i.e either superclass or subclass reference.
[This message has been edited by Swati Samudra (edited January 18, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain this code