my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shadowing" Watch "shadowing" New topic
Author

shadowing

harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
can any one explain shadowing properly with example,like wat happens with the variables and the methods which one is executed


Harish<br />SCJP 1.4 (85%)
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Hi Harish,

Look at the following code:
code:
-----------------------------------------------------------------------
class Base{
int i=10; //1
public void display(){
System.out.println("i: "+i);
}
}

class Derived extends Base{
int i=20; //2
public void display(){
System.out.println("i: "+i);
}

public static void main(String a[]){
Base b1=new Base();
Derived b2=new Derived();

System.out.println(b1.i); //3
System.out.println(b2.i); //4

}
}
-------------------------------------------------------------------------
Now in the above program, int declared at //1 is shadowed by integer declared at //2. Similarly method display() declared in Base is shadowed or you can say overridden by the derived class.
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Consider another simpler example:


Here, the method shadow() is shadowing the class member variable x with it's own local variable x. So if you refer to x directly, you will be accessing the local method's variable x. Hence, if you wish to explicitly use the class variable x, you will have to use the this.x statement.

Try removing line 1 from the code and see what happens. The code now accesses the class variable x. You so not need the this construct now.

That much about variable hiding/shadowing.

Methods are never shadowed. They are either overloaded or overridden to implement shadowing.

Hope this helps


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
sorry Ritu i pasted the same code which u gave me,
here is the thing wat i expected,thanks anyway,try this
class Base
{
int i=10; //1
public void display(){
System.out.println("i: "+i);
}
}

class Derived extends Base{
int i=20; //2
public void display(){
System.out.println("i: "+i);
}

public static void main(String a[]){
Base b1=new Base();
Base b2=new Derived();

System.out.println(b1.i); //3
System.out.println(b2.i); //4
b1.display();
b2.display();
}
}
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by harish shankarnarayan:
sorry Ritu i pasted the same code which u gave me,
here is the thing wat i expected,thanks anyway,try this
class Base
{
int i=10; //1
public void display(){
System.out.println("i: "+i);
}
}

class Derived extends Base{
int i=20; //2
public void display(){
System.out.println("i: "+i);
}

public static void main(String a[]){
Base b1=new Base();
Base b2=new Derived();

System.out.println(b1.i); //3
System.out.println(b2.i); //4
b1.display();
b2.display();
}
}


output
------------------------
10
10
i: 10
i: 20
------------------------

Harish here in the above code you have initilized the variable of Base class with the Derived class.
you must be knowing that selection of methods depends on the runtime object but selection of variables depends on the variable
so here since the variable "b2" is of the type Base calling "b2.i" would print the value 10 that is the value of i in class Base.

On the other hand calling the method display depends on the Runtime object of the variable. and here the runtime object referenced by the variable b2 of that of class Derived, so the Method of Derived class would be called and that will ofcourse print the value of i in its own class.

hope this would help you.

Sandy
[ September 13, 2005: Message edited by: Sandeep Chhabra ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
This is overriding not shadowing.


Sherry Jacob posted good example of shadowing.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: shadowing
 
Similar Threads
Overriding Vs Shadowing
Odd NullPointerExceptions
Shadowing Variable
Shadow variables
Static and Instance fields