• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

shadowing

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is overriding not shadowing.


Sherry Jacob posted good example of shadowing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic