• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variable hide inside method

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Kirving{
private final int i = 10;
public static void main(String argv[]){
Kirving ki = new Kirving();
ki.go(3);
}
public void go(int z){
int i=z;
}
}

The above code compiled without error.My question is i is private variable declared but again same i is used inside go mehtod that means i was hidded inside go method or not?
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its just hidden, does this help ...



On my pc gave ...

local i =3 member i = 10
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method and variable hiding:
Hi Praphakaran,

The term shadowing when your parent class has a member variable named x for example and in the sub class you are also creating a member with the same name, means shadowing the parent class member x. This is bad practice although. One thing must be clear that member variables are not overridden. Overriding and shadowning are two completely different things. While you talk about overriding you have polymorphism in hand to avail with. Let us see the code:

class Base {
int x=10;

public void demo() {
System.out.println(�demo() of base class�);
}
}
Class subclass extends Base {
int x=20;

public void demo() {
System.out.println(�demo() of subclass�);
}
public static void main(String[] args) {
Base b1 = new subclass();

System.out.println(b1.x);//prints 20
B1.demo();//calls the subclass version(overridden version)
}
}

This may give the clear cut idea of shadowing and overridding.

So far as the term hiding is concerned we can also see that in the term when we redefine the method in the subclass that is static. I said redefine and it never means overriding because static methods can�t be overridden.

class Base1 {
static void display(){
System.out.println(�Base1 class display()�);
}
}




class Subclass1 extends Base1 {
static void display() {
System.out.println(�Subclass1 class display()�
}

public static void main(String[] args) {
Base1 b1 = new Base1();
b1.display();//base1 class�s method display() is called.
Base b2 = new subclass1();
b2.display();//base1 class version display() method is called

subclass1 s1 = new subclass1();
s1.display();//the subclass1 version of method display() called

}
}

In this case the reference variable matters instead of run type type of Object the reference variable holds. Although you will get sort of warning that static method should be called in static way means using class name but calling a static method using reference variable gives you no harm but leaves your code less readable. You should call static methods in the following way:

Base1.display();//proper way to call static method
Subclass1.display(); //proper way to call static method


I hope this little explanation may help you!
With regards,
cmbhatt
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

class Base {
int x=10;

public void demo() {
System.out.println(�demo() of base class�);
}
}
Class subclass extends Base {
int x=20;

public void demo() {
System.out.println(�demo() of subclass�);
}
public static void main(String[] args) {
Base b1 = new subclass();

System.out.println(b1.x); //prints 20
B1.demo(); //calls the subclass version(overridden version)
}
}

you made a little mistake on the previous program.i have corrected it with displaying in bold letters.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to disagree.

When an instance variable is hidden in a subclass, and you access that variable using a reference, then the type of the reference, not the runtime type of the object, determines which instance variable is chosen.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers !

I think thats correct, since It's an idea of variable hiding, it's a whole new re-declaration for variable. let me give you an example.

class A {
final int i = 10;
}

class B extends A {
int i = 100; //you can have this I re-declared
} //but as you remove INT it will give you
// an error re-defining is possible, but not
class runA { // modification
public static void main(String []args)
{
B b = new B();
A c = new B();
System.out.println("I.."+ c.i);
}
}

I hope it helped

- thanks
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiva Mohan,



Hi Chandra,

class Base {
int x=10;

public void demo() {
System.out.println(�demo() of base class�);
}
}
Class subclass extends Base {
int x=20;

public void demo() {
System.out.println(�demo() of subclass�);
}
public static void main(String[] args) {
Base b1 = new subclass();

System.out.println(b1.x); //prints 20
B1.demo(); //calls the subclass version(overridden version)
}
}

you made a little mistake on the previous program.i have corrected it with displaying in bold letters.



I am curious to know, on what behalf you say the line (done bold), will print 20. I think, you in the dilemma of the member variable overriding or polymorphism applying on member variables to.
Member variables are shadowed and not overridden. You access member variables using reference variable. It is nothing like that the reference variable is referring to subclass object so at run time the subclass member variable will be selected. Not at all. Shadowing and overriding do have difference of heavens.

And finally...
System.out.println(b1.x); will print 10 NOT 20



Thanks and Regards,
cmbhatt
[ April 06, 2007: Message edited by: Chandra Bhatt ]
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic