Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheriting a protected member

 
Roel De Nijs
Sheriff
Posts: 9180
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a look at following hierarchical tree:



why i can declare in class C a private member with the same name as the inherited one, because class C has already a member number (inherited from B). or is this something like method overriding but with variables? and can i access the inherited member in class C
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concept is called hiding. Declaring a field with the same name hides the one declared in the superclass, just like declaring a variable inside a method would hide a field of the same name declared outside of the method.

The field declared in the superclass can be accessed via "super.number".
 
Roel De Nijs
Sheriff
Posts: 9180
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just like declaring a variable inside a method would hide a field of the same name declared outside of the method = shadowing

and is hiding and shadowing synonyms?
 
Amar Shrivastava
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is beacuse class D extend C and inherits all members except which are private, final,overridden or hidden.

A subclass does not override fields of superclass, but it hides them.The subclass can define fields with the same name as in the superclass. Doing this hides the superclass field and hence it is not inheritted.
In subclass we can use superto access supeclass members(including hidden fields). If the hidden field is static then it can also be accessed by the superclass name.
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roel De Nijs:
just like declaring a variable inside a method would hide a field of the same name declared outside of the method = shadowing

and is hiding and shadowing synonyms?


I've never heard it called anything but hiding. If you're calling it shadowing I suppose that makes them synonyms, yes.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the access modifiers (protected and private in this case) have nothing to do with it. If they are both public, you will have the same result.

Layne
 
Amar Shrivastava
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne, I suppose that here in this example it is the modifier private in class C which is the main culprit for the error in class D.

This field in C is hiding the superclass field and so not inheritting it,
and since it is private it is not letting class D to inherit this field.
 
Roel De Nijs
Sheriff
Posts: 9180
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amar Shrivastava:
The error is beacuse class D extend C and inherits all members except which are private, final,overridden or hidden.


Why a public final method can't be inherited? It can't be overridden, but class D will inherit all public and protected members of class C (even when they are final)
 
Amar Shrivastava
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely right Roel. Sorry, the final modifier is the odd one. Thanks for reminding.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amar Shrivastava:
Layne, I suppose that here in this example it is the modifier private in class C which is the main culprit for the error in class D.

This field in C is hiding the superclass field and so not inheritting it,
and since it is private it is not letting class D to inherit this field.


What kind of error are you referring to? Is it a compiler error or a runtime "error"? Have you tried changing "private" in class C to "public"? You might also try chaning the modifier for the field in class D. Use different combinations to see what happens. I am pretty sure that you will get the same results? This should illustrate that "private" is NOT the main culprit.

Layne
 
Roel De Nijs
Sheriff
Posts: 9180
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne,

I did what you said, so changing the access modifier of member in class C and then checking in class D of member is visible or not. this are the results:

- private --> not visible
- default --> not visible (class D was in other package than class C)
- protected --> visible
- public --> visible

so that isn't prooving the point you made
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laynes point was that the important concept of the original question is hiding, i.e. the fact that the variables have the same name, and not accessibility, which of course depends on the modifier.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roel De Nijs:
Layne,

I did what you said, so changing the access modifier of member in class C and then checking in class D of member is visible or not. this are the results:

- private --> not visible
- default --> not visible (class D was in other package than class C)
- protected --> visible
- public --> visible

so that isn't prooving the point you made

As my Ulf says, my point is about which field named number is available. If C has a field named number, then the access modifier for the number field in A is completely irrelevant unless you try to access it explicitly. In other words, "this.number" in a method in C will ALWAYS refer to the number field in C. It will NEVER refer to the number field in A, no matter what access modifiers are used.

Perhaps I have misunderstood your question. If so, please restate the question.

Regards,

Layne

[ October 21, 2005: Message edited by: Layne Lund ]
[ October 21, 2005: Message edited by: Layne Lund ]
 
Roel De Nijs
Sheriff
Posts: 9180
77
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, that was the question.

get your point now.

thanks for the explanation
 
Amar Shrivastava
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Layne you are absolutely right. I misunderstood the Question.

If the number field in C was not private then D would have inheritted that field but NOT the field in A.

Thanks for explanation.
[ October 21, 2005: Message edited by: Amar Shrivastava ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic