File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shadowing

 
harish shankarnarayan
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one explain shadowing properly with example,like wat happens with the variables and the methods which one is executed
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is overriding not shadowing.


Sherry Jacob posted good example of shadowing.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic