Can I make a superclass's variable static final in subclass?
13 Replies
⌛
This post has been reserved for your question.
Hey @sadstephen! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
you cannot replace variables of subclasses
and especially not make them static or final
the only thing you can do is assigning them in the constructor or creating more variables
I just want to understand what is the effect of doing that - we have a definition of instance variable about in superclass; then in subclass we are creating a static variable in the same variable name.
Good question. The effect should be that both separate variables still exist, but the one in the subclass shadows the one in the superclass.
So if you write
about
in the subclass, it will refer to Bots.subclass
.is it called polymorphism?
No.
or encapsulation or something 😦
Polymorphism is when you have an instance of a subclass in a superclass-type variable.
Encapsulation is just when one object is a field in another.
When these variables use the same name, you get shadowing, but the names being the same doesn’t result in encapsulation or polymorphism.
Thank you for your time. ❤️
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
it results in you having two variables with the same name as mentioned by Snake, yes
if you access the variable from
User
, you get User#about
and from Bots
, you get Bots.about
I think about is not static variables in superclass User.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.