• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question about instance variables

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm studing for SCJP exam. So I wrote this simple code and a doubt appeared.

Given:

class A {
public int number = 100;
public int getNumber() {
return number;
}
}

class B extends A {
public int number = 200;
}

class Test {
public static void main (String ... args) {
A a = new A();
A b1 = new B();
B b2 = new B();
System.out.println("a = " + a.getNumber());
System.out.println("b1 = " + b1.getNumber());
System.out.println("b2 = " + b2.getNumber());
}
}


with this output:

a = 100
b1 = 100
b2 = 100

Why is the instance variable value 200 not being used?

Thanks Ranchers,

Leonardo Luiz
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variables are not overridden so when the getNumber() method is called it returns number from class A. You might try overriding the getNumber() method in class B and see if the output gives you what you initially expected to see. Like this:
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I will give you some code! I think you will know all about it !
class Test{
String s ="Hello!";
void show(){
System.out.println(s);
}
}
class TestSubClass extends Test{
String s = " Ranch friend!"
}
public class Kown{
Stirng s ="I'am Chinese!"
public static void main(String[] args){
TestSubClass t = new TestSubClass();
t.show()
}
}
It's print "Hello!" If you want print another ,you can overriding method show in subclass ! The show() will print out that where you put !
I hope my example will help you !
 
Leonardo Luiz
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you.

I understand that ´┐Żinstance variables are not overridden´┐Ż. Creating an overridden version of this method the sub class instance variable is used.

So, when a class has an instance variable with the same name that a super class instance variable. The methods of this class have no access to super class variable.
Ok?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Leonardo Luiz:
... So, when a class has an instance variable with the same name that a super class instance variable. The methods of this class have no access to super class variable. Ok?


This is a case of "variable hiding." (See JLS 8.3.3.1.) The hidden variable can be accessed via super.

In the code below, x is hidden by being re-declared in the subclass. However, within the overridden method getX, both x variables can be accessed by using super to access the hidden variable.
 
Leonardo Luiz
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variable hiding! So "super" is the key to access a variable in this case.
I will spend more time with this topic.

Thanks a lot!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just noticed that the example I linked to above (8.3.3.1) is for a hidden class (static) variable. An example of a hidden instance variable is just below that, under 8.3.3.2. The idea is the same, but a more detailed explanation is given under 8.3.3.2.
 
Leonardo Luiz
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for explanation.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic