Simple Java Trace Code

Hi, I'm a student and I'm unable to brain the answer behind this question 😔 (answer isn't provided)
No description
No description
11 Replies
JavaBot
JavaBot•2w ago
⌛ This post has been reserved for your question.
Hey @Lumarc! 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.
Jordan σ_σ
Jordan σ_σ•2w ago
I will be butchered for giving the answer here but this is pretty simple just lowkey hate how it is written. Lets break it down though and hopefully the Java Helper Gods can +1 my explanation here: The output is printing System.out.println(myA.doA() + myA.doA2() + myA.a); myA.doA() would be coming from where exactly??? Lets take a look at the object thats being instantiated above the print method. A myA = new B(); Take a look here. We're taking myA as an object thats referencing A but we are practically hooking it into the B class. So our myA object will use B methods. Since the method String doA() { return "b1 "; } overrides the first doA() method from class A. Class B's doA() method will be used or in other words take priority. Why does this happen? Because B class inherits A class. Also given the multiple choice answers thats given it doesn't seem hard to figure that one out given that a2 is being returned from method protected static String doA2() { return "a2 "; } right? So that means that a2 would be the second inputted value yes? And then well...the last one is quite simple as well... myA.a is simply just referring to the object's defined field. If we go back to the code and see where thats at...you'll see its int a = 5 and then well that should answer the question entirely. Hope I don't get butchered for giving out the answer of course...but wanted to explain it thoroughly so you can understand it!
dan1st
dan1st•2w ago
To say it simply: overriding only happens for non-static methods - for static methods and fields, you'd just have copies of the members
Jordan σ_σ
Jordan σ_σ•2w ago
yes 😅 thanks dan
Lumarc
LumarcOP•2w ago
wahh thanks a lot for the lengthy explanation! 🤩 My confusion is it being 5 rather than 7, as since it's hooked to B class, I thought it will override and output 7 instead? Definitely my understanding of how declaration of variables / data fields work is wrong here :idontunderstand: or as @dan1st | Daniel said, the a field in class B is just a copy and has no relation with a in class A? If yes, how do I call 7?
dan1st
dan1st•2w ago
to get B#a (which is 7), you either need a method in B (that method could override another method from A) that accesses a or you could have a variable of the static type B:
B b = new B();
System.out.println(b.a);//7
B b = new B();
System.out.println(b.a);//7
variables aren't subject to overriding you can access variables in subclasses but if you create a variable with the same name in both the superclass and the subclass, you have two copies of the variable If you want a subclass to set a superclass variable to some value, you can do that in the constructor:
class C extends A{
public C(){
a=7;
}
}
class C extends A{
public C(){
a=7;
}
}
Then
A a = new C();
System.out.println(a.a);//7
A a = new C();
System.out.println(a.a);//7
Lumarc
LumarcOP•2w ago
got it, so the A myA = new B(); is the reason why it's 5, causing polymorphism for only instance methods, right?
dan1st
dan1st•2w ago
yes though specifically inheritence - there are also other types of polymorphism
Lumarc
LumarcOP•2w ago
Alright noteddd, thanks to bothh of ya 🤩
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.
JavaBot
JavaBot•2w ago
Post Closed
This post has been closed by <@583606051476340736>.

Did you find this page helpful?