aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why isn't it compiling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why isn Watch "why isn New topic
Author

why isn't it compiling?

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Question ID :957672686580
Consider following two classes:
//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() );
}
}
What will be the output of compiling and running class B ?

Ans : It will not Compile.
Can any one explain me with reasons saying why doesn't the above code compile?
Sonir
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
It won't compile since
i is a protected variable of class A
and in class B you are trying to access it using the superclass (A) reference variable.
If the subclass is in another package, it can access the superclass's protected varibles only by subclass's ref variables.
[ January 07, 2002: Message edited by: Vanitha Sugumaran ]
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
can you explain "subclass's ref variables"?
thanks


help you means help me
Prashanth menon
Ranch Hand

Joined: Feb 20, 2001
Posts: 65
Hi
If you modify process(A a) as
process(){
i=i*2;
}
it will work. It is the i of the instance of B(that it inherits from A). Now you see no parameter need be passed in such a case, provided you are using it call other functions.
First thing to understand is that the class is only the blueprint. The objects are built based on it. As known , a protected variable is not accessible from any class other than their subclass when in other packages..I Propose to create an instance a of B, and then b of B. I cannot access the protected i of a from within b. I hope it is clear.
Now even if you changed this method, as I told it will give the result as 10 only.
If you call b.getI() it gives 20.
Please note also that if p1 is imported using wildcard(import p1.* we neednot use p1.classname directive to refer to the classes in p1.
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
I have a doubt. I modified the above code to following. It is compiling fine
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() );
}
}
But if I make class A as public its not compiling.Even when i saved it in file A.java, while compling B.java it says class A not found.
Am bit confused here. Can anybody give clear idea on this. The above code will be in same package. So if it is in same package can we access protected variables using super class reference.
[ January 08, 2002: Message edited by: Arathi Rajashekar ]

Arathi<br />Sun Certified Java Programmer
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

But if I make class A as public its not compiling.Even when i saved it in file A.java, while compling B.java it says class A not found.

What compiler are you using.
It compiled clean for me when I stored them in seperate files. If you have A.class (of some kind) from your previous compile, delete the class file and give it another try.
Thats the best I can think of.
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Prashanth menon
Ranch Hand

Joined: Feb 20, 2001
Posts: 65
Originally posted by Arathi Rajashekar:
I have a doubt. I modified the above code to following. It is compiling fine
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() );
}
}
But if I make class A as public its not compiling.Even when i saved it in file A.java, while compling B.java it says class A not found.
Am bit confused here. Can anybody give clear idea on this. The above code will be in same package. So if it is in same package can we access protected variables using super class reference.
[ January 08, 2002: Message edited by: Arathi Rajashekar ]


Your code compiled because the classes are in the same package. Protected variables comes next to public in access. If in same package they can be accessed from every classes, not just the subclasses.
There should be no problem compiling A.java when it contains public class A.
But if class A 'cannot be found' when compiling B then please check wheather its available in your classpath. You might have put it in a package and not imported it. Or you may have overwritten the classpath.
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
I still confused:
you said:a protected variable is not accessible from any class other than their subclass when in other packages
class B is subclass of class A,why cannot it accesses the variables of A?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Seany Iris:

class B is subclass of class A,why cannot it accesses the variables of A?

IF class B AND class A are in the same
package then class B can always access class A's non-private members.
IF class B and class A are in different packages, then class B can only access class A's public and protected members. Any members defined with the default access modifier (ie, no explicit modifier present) are NOT ACCESSABLE to subclasses outside of its package.
Rob


Rob
SCJP 1.4
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
B is the subclass and it is in a different pckg.
'i' is inherited in B and it is protected as well..
So this combination above, make the accessing only possible through B's reference
Ragu
Bob E. Lee
Ranch Hand

Joined: Jan 15, 2002
Posts: 36
Thx guys!!
I was lost when I first look at the question. But after writing out the example and read each post x10 I finally got it! It's interesting ....
I'll have to make a note about this since it doesn't fit into my matrix.
Bob
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Here's a slightly modified version of the original code:

I chaned the process() method. In this example, the test
a instanceof B
returns true, because when we call
b.proccess(a)
in main, we are passing in an instance of class B.
By casting the argument from class A to B, we can see that we are allowed to now access the class' protected data member, because we are doing so via a reference to a B class, which contains the inherited int i as a member.
Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why isn't it compiling?