my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default access variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default access variables" Watch "Default access variables" New topic
Author

Default access variables

Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
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

Joined: Mar 07, 2010
Posts: 446
    
    1

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

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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?...


SCJP 6 [SCJP - Old is Gold]
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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

Joined: Jul 11, 2010
Posts: 247

What if?

Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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

Joined: Aug 29, 2010
Posts: 19
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

Joined: Jul 11, 2010
Posts: 247

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

Joined: Jan 22, 2007
Posts: 400

Now this is the another topic you should start another thread for this and you'll get many thoughts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default access variables