Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why isn't it compiling?

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain "subclass's ref variables"?
thanks
 
Prashanth menon
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Prashanth menon
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic