• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default access variables

 
Sven Mathijssen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if?

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this is the another topic you should start another thread for this and you'll get many thoughts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic