Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Why does th compiler allow changing the type of inherited instance/static variables.

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

After reading the frist 2 chapters of SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) by Katherine Sierra and Bert Bates, I have a question on inheriting public variables. This is my own question, based on what I understood in the first two chapters.



class A{

public int variableA=10;

public float variableB=15;

}

class B extends A{

public boolean variableA= false;

}

class TestVariableInheritance{

static public void main(String[] args){

A arefb = new B();

System.out.println( " variableA from class A is : " + arefb.variableA );
//Polymorphism works only on instance methods, not instance variables
//The above should print 10 . no problem.

B onlyb = new B();

System.out.println( " inherited (not overridden) variableB in class B is " + onlyb.variableB );

}

}


1) variableB is inherited in class B , for this reason onlyb.variableB prints a value.

2) Similarly I expect variableA to also be inherited in class B , however
I am able to define a new variable with the same name variableA , but with a different type ( the type is changed from int to boolean)

The compiler allows me to change the type of the inherited variableA to anything, why?

I was expecting the compilation to fail, but why does the above code compile?
[ December 11, 2008: Message edited by: Rashmi Jaik ]
 
Rashmi Jaik
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No one knows?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) variableB is inherited in class B , for this reason onlyb.variableB prints a value.

2) Similarly I expect variableA to also be inherited in class B , however
I am able to define a new variable with the same name variableA , but with a different type ( the type is changed from int to boolean)

The compiler allows me to change the type of the inherited variableA to anything, why?



Well.... Technically, polymorphism only applies to methods, not instance variables. You are not overriding or inheriting the variables. These are two different variables, with the same name, and different type. You can still get to the variable of the base class, by casting to the base class, before dereferencing.

Now... I understand why you may think inheritence is occuring. If you use a variable that doesn't exist in the subclass, the compiler is smart enough to assume that you meant one of the variables in the base class, and generate the right code.

Henry
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B onlyb = new B();
onlyb.variableA prints false.
I want to know is it possible for onlyb refernce to access the int variableA declared in the super class. ( i guess NO)
If not is it because of shadowing ??
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Tharakan:
B onlyb = new B();
onlyb.variableA prints false.
I want to know is it possible for onlyb refernce to access the int variableA declared in the super class. ( i guess NO)



Did you try it out?


No you have'nt. The above code works fine and gives the value 10 after you comment the line as shown in the above code and do


Try it out and then try finding out the answer based on the above explanations.You will find the answer there.
[ December 11, 2008: Message edited by: Sudipto Shekhar ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i was try is


This is what i tried.
It prints false.
Now i want to access the int variableA(Without commenting base class variableA).Is it possible .If so .how?
If not, is it shadowing??
[ December 11, 2008: Message edited by: James Tharakan ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible if you upcast the onlyb and assign it to A reference.

The above code will print 10.
And you can't directly access variableA of A class through B reference, because B is hiding the superclass version of variableA with it'S own version.
 
Rashmi Jaik
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


Well.... Technically, polymorphism only applies to methods, not instance variables. You are not overriding or inheriting the variables. These are two different variables, with the same name, and different type. You can still get to the variable of the base class, by casting to the base class, before dereferencing.

Now... I understand why you may think inheritence is occuring. If you use a variable that doesn't exist in the subclass, the compiler is smart enough to assume that you meant one of the variables in the base class, and generate the right code.

Henry



Thank you Henry, you have answered my question.
 
Rashmi Jaik
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajshekhar Paul:
It's possible if you upcast the onlyb and assign it to A reference.

The above code will print 10.
And you can't directly access variableA of A class through B reference, because B is hiding the superclass version of variableA with it'S own version.



If you have B onlyb = new B();

and if you cast ((A)onlyb).variableA then you get the variable in class A, I guess this is the same as what you mentioned.

If we try to access an overridden instance method as in ((A)onlyb).instanceMethodA() , then it will invoke the overriding method not the overridden method. because B is not hiding instanceMethodA() it is overriding it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic