• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected access??

 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I've this code. I'm not able to figure out why in class ATest after oveeriding method1 I'm getting null? Shouldn't I get value of a from super class?
I'm lost. Can any body plz help me out?
Thanks
-PC
-------------------------------------------------------
class B {
protected void method1(Object a) {
a = new Object();
//a="raj";
}
}

public class ATest extends B {
public static void main(String[] args){
B b=new B();
Object g = null;
b.method1(g);
System.out.println (g); // why it gives me null??
}
}
---------------------------------------------------------
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're a little confused about how parameters are being passed.
When you have the line:

You have now invoked the method method1 and sent a reference to the object referenced by g. If we draw a picture, we see that we have something like this:

Now, in method1, you execute this line:

As we're now assigning a new reference to a, we change our picture to reflect that. It now looks like this:

Notice that g is unchanged. Therefore, we end up with null being printed out.
I hope that helps,
Corey
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey, its clear now.
I appreciate your help
-PC
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic