I have a States class that has a couple of variables, and i have a child class that extends from the super class how can i set the variables in the child class so the superclass recognizes it, because right now if i use a method inside the superclass to access the variables inside the child class it returns a null
#How to set default value for a child class
32 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @orchid egret! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Well parent classes dont inherit from child classes.
so is it not possible to set the values without a constructor?
what do you mean? you can of course access all the variables of the super in the child class. Or what do you want to do?
so what i used to do is using this constructor to set the super values, is it possible to do the same thing without it being in a constructor?
just use super.
im so sorry im pretty new, why does this happen?
your fields in your superclass may not be private.
Here is a list of modifiers you can apply to fields with their resulting visibility
that means that for your subclass to "see" the variables of the superclass these fields must be declared as protected not private
ohhhh thankyou very much, thankyou
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.
i will try
use getter / setter method
sorry im a little stupid, maybe i missed some of the basics somewhere.
uh let me give a full context then because making it protected did not work
i have a class where i had to make all the states object as you can see the Selangor selangor lines
now previously i used Selangor selangor = new Selangor("Selangor", 10, 10, 10) as a parameter
and i used a constructor like this to set the super class values
but now i realized a flaw, every time the class is relaunched, the objects are reinstantiated meaning all the values are reset
so im trying to make it so that the Selangor class objects have a default value like this
but now the super class does not inherit the values from the child class
Well do your parents inherit traits from you?
No, they don't. Inheritance is a one way thing.
this is the parent class
ohhh
how do i make them inherit from a child class
im starting to think this is all just a big design failure from my part
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.