#How to set default value for a child class

32 messages · Page 1 of 1 (latest)

orchid egret
#

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

sly oarBOT
#

This post has been reserved for your question.

Hey @orchid egret! Please use /close or the Close Post button 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.

dusk ether
orchid egret
#

so is it not possible to set the values without a constructor?

dusk ether
#

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?

orchid egret
#

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?

dusk ether
#

just use super.

orchid egret
#

im so sorry im pretty new, why does this happen?

chilly spire
#

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

orchid egret
#

ohhhh thankyou very much, thankyou

sly oarBOT
# orchid egret 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.

orchid egret
#

i will try

dusk ether
#

use getter / setter method

orchid egret
#

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

dusk ether
#

Well do your parents inherit traits from you?

#

No, they don't. Inheritance is a one way thing.

orchid egret
#

this is the parent class

orchid egret
#

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

orchid egret
#

ok i might just failed the entire fx app i made for fun lol

#

aight thanks guys

sly oarBOT
# orchid egret aight thanks guys

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.