#Doubt with constructor

1 messages · Page 1 of 1 (latest)

forest geode
#

Hey guys!! Currently practicing inheritance and i have a doubt. since i initiated the values of the attributes of the superclass in it's constructor is it necessary to do what i did and initiate the values of the superclass attributes again or by only calling the constructor of the superclass in the subclass does it??

slate anchorBOT
#

<@&987246399047479336> please have a look, thanks.

slate anchorBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

potent storm
#

depends on the variable definition, if private then u need to

#

if they are protected, then the child classes have access aswell

#

(but the second shot, our duplicating your code already )

forest geode
#

yeah . all my attributes are private since ive been told that using protected fields is a bad practice

potent storm
#

depends on use case usually

#

but in your second shot, u call the constructor, and then the setter methods.

forest geode
potent storm
#

if there is no logic or logging active, use the constructor, else go by the setters

potent storm
forest geode
#

ok. in my case they only set the value of the attributes .

potent storm
#
public void setName(var name){
logger.info("name set to: " + name );
this.name=name;
} 
slate anchorBOT
potent storm
#

if its just setting, and there is a constructor available for the combination, prefer that one

#

if there isnt, have a look on method chaining 😉

forest geode
#

ok

woeful seal
#

you should call the super constructor

#

which should take care of the setting

forest geode
#

ok. Thanks a lot man !!