Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Default access variables

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

The following has been bothering me for some time. Suppose we have two classes, Child and Parent, both in the same package, defined as follows:

Class Parent:



Class Child extends Parent:



This prints:


Value of x is: 10



I would think the variable x is inherited from Parent and would lead to a duplicate definition of the same variable in the Child class. After all, you cannot define the same instance variable twice in the same class (the compiler complains about a duplicate field error). Although I am able to access the superclass variable by calling super.x, i'm still confused as to whether this is a case of somehow shadowing a variable or anything.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Child extends Parent

this statement does not mean that the members of the Parent class are copied into the Child class
it just means that all the members of Parent are available to Child through inheritance
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sven Mathijssen wrote:
I would think the variable x is inherited from Parent and would lead to a duplicate definition of the same variable in the Child class. After all, you cannot define the same instance variable twice in the same class (the compiler complains about a duplicate field error). Although I am able to access the superclass variable by calling super.x, i'm still confused as to whether this is a case of somehow shadowing a variable or anything.




Yes this is the case of variable shadowing.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my opinion,

Properties is specific to each class... There are some properties differ based on the context even though the property having the same name but of different types (or) same type but differ in usage from each other will be in both super and sub class... So Properties (or) instance variables cannot be overridden. So, they are binded up with object based on their class type...

Please correct!!!

This is a question i have always... Why Properties are'nt Overridden?... Could anybody explain with a practical analogy?...

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:As per my opinion,

Properties is specific to each class... There are some properties differ based on the context even though the property having the same name but of different types (or) same type but differ in usage from each other will be in both super and sub class... So Properties (or) instance variables cannot be overridden. So, they are binded up with object based on their class type...

Please correct!!!

This is a question i have always... Why Properties are'nt Overridden?... Could anybody explain with a practical analogy?...



Actually, I did not get your question properly

But you can change the value of inherited instance variables value from child class try the following code
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if?

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now this is good question.

Remember, variables in Java are resolved by the reference type always not the object they are referencing just opposite to the method overriding.


Hope this helps

Minhaj
 
Sven Mathijssen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:
What if?

<code above>



Good question and this adds to my confusion on the subject. My first guess is that by inheritance rules, the overridden Child.testIt() method is used at runtime (because the class is essentially a Child at runtime), but because childRef has reference type Parent, the childRef.x method returns the reference type Parent's value of x.

It appears that dynamic binding applies only to overridden methods of the supertype, not to instance variables.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Minaj...

Could you please explain why this kind of rule is imposed by Java Creators?... Flexibility which we have to override methods, why not for instance variables?....
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now this is the another topic you should start another thread for this and you'll get many thoughts.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic