Need help with a this statement question | beginner

Hey guys Im a beginner in Java and I have a question regarding constructors and inheritance. If I have a constructor in a subclass am I able to use a this statement in a constructor and a super() in the next constructor?
22 Replies
JavaBot
JavaBotβ€’2w ago
βŒ› This post has been reserved for your question.
Hey @v! 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 marked as dormant 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.
iLikeLemon_
iLikeLemon_β€’2w ago
Yes
Hosti
Hostiβ€’2w ago
You can do this
Public consName(){
super("myName")
this.age = 5
}
Public consName(){
super("myName")
this.age = 5
}
This message has been formatted automatically. You can disable this using /preferences.
iLikeLemon_
iLikeLemon_β€’2w ago
The age variable is only accessible through the any object of the child class (subclass) only and not in any object of the parent class
v
vOPβ€’2w ago
Say I have two constructors, and the first constructor does not have super() defined, but it has a this statement defined, will it run the next Constructor?
Hosti
Hostiβ€’2w ago
If you have multiple methods in the same class with the same name, which method will run depends on which order of parameters you gave so lets say this:
Class child extends parent{
public child(String name){
super(name);
}

Public child(int age){
this.age = age;
}
Class child extends parent{
public child(String name){
super(name);
}

Public child(int age){
this.age = age;
}
This message has been formatted automatically. You can disable this using /preferences.
iLikeLemon_
iLikeLemon_β€’2w ago
Now if i did this: Child myChild = new Child("NAME"); the first constructor will work How ever if i did this: Child otherChild = new Child(8); The second constructor will run Sorry typing is not the best and im slown im just on phone
v
vOPβ€’2w ago
Dw you're good
iLikeLemon_
iLikeLemon_β€’2w ago
So if u have multiple methods with the same name which one will run depends on you order the arguments in the constructor call or method call I recommend learning through codecademy's learn java course
v
vOPβ€’2w ago
I am 86% done
iLikeLemon_
iLikeLemon_β€’2w ago
Great job
v
vOPβ€’2w ago
Thank you, it's been fun
JavaBot
JavaBotβ€’2w ago
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.
iLikeLemon_
iLikeLemon_β€’2w ago
Also i recommend watching code with john on youtube for oop
v
vOPβ€’2w ago
I am watching Kevin something I forgot his name let me see
iLikeLemon_
iLikeLemon_β€’2w ago
Lol
v
vOPβ€’2w ago
Alex Lee Im watching Alex Lee, and Bro Code lol
iLikeLemon_
iLikeLemon_β€’2w ago
Bro code is amazing but takes long to explain sometimes Best of them all specificly for java is coding with john He explains ssoooo simply and clearly and in 3-12 minute videos If you have any other questions DM meπŸ‘
v
vOPβ€’2w ago
bet πŸ˜„
JavaBot
JavaBotβ€’2w ago
πŸ’€ 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.
???
???β€’3d ago
Hack ? Pay 10 dollars for unban hack
JavaBot
JavaBotβ€’3d ago
πŸ’€ 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.

Did you find this page helpful?