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??
#Doubt with constructor
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
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 )
yeah . all my attributes are private since ive been told that using protected fields is a bad practice
depends on use case usually
but in your second shot, u call the constructor, and then the setter methods.
so calling the constructor is the only thing i need to do then??
if there is no logic or logging active, use the constructor, else go by the setters
depends on what there is happening beneath the hood. If its just setting a variable, then the constructor is prefered. If the set methods do extra things, use those
ok. in my case they only set the value of the attributes .
public void setName(var name){
logger.info("name set to: " + name );
this.name=name;
}
Detected code, here are some useful tools:
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 😉
ok
ok. Thanks a lot man !!