Simple Java Trace Code
Hi, I'm a student and I'm unable to brain the answer behind this question 😔 (answer isn't provided)
11 Replies
⌛
This post has been reserved for your question.
Hey @Lumarc! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
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!To say it simply: overriding only happens for non-static methods - for static methods and fields, you'd just have copies of the members
yes 😅 thanks dan
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?
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:
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:
Then
got it, so the A myA = new B(); is the reason why it's 5, causing polymorphism for only instance methods, right?
yes
though specifically inheritence - there are also other types of polymorphism
Alright noteddd, thanks to bothh of ya 🤩
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.
Post Closed
This post has been closed by <@583606051476340736>.