Page 1 of 1

About Question enthuware.ocajp.i.v7.2.998 :

Posted: Sun Sep 15, 2013 5:52 am
by TheSarjo
I do not understand this part of the explanation of the question:
...Therefore, at compile time, compiler assumes that g.play() might throw an exception, because Game's play method declares it...
I mean, i've studied that methods bind at runtime, not at compile time (while variables compile at compile time).

So if we have

Code: Select all

class A
{ void doSomething() throws Exception {}  }

class B extends A
{ void doSomething() {} }

class Test
public static void main(String [] args)
{ A a = new B();
a.doSomething();
}
}
a.doSomething() should refer to doSomething in class B.
But in the exercise, it gives a compile error... don't understand why (if it's true that methods bind at runtime).
Does the method in class B silently inherits the thrown Exception? Why?
thank you very much for your attention!

Re: About Question enthuware.ocajp.i.v7.2.998 :

Posted: Sun Sep 15, 2013 5:59 am
by admin
TheSarjo wrote: a.doSomething() should refer to doSomething in class B.
No, as you said, actual binding occurs at runtime. So the compiler has no idea that a will point to an object of class B. So why would a.doSomething refer to B's doSomething? Therefore, the compiler uses A's doSomething's signature while compilation.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.998 :

Posted: Mon Sep 16, 2013 7:56 am
by TheSarjo
Ok! got it
thank you!

Re: About Question enthuware.ocajp.i.v7.2.998 :

Posted: Sat Feb 18, 2017 7:33 am
by Deleted User 3513
Just wanted to clarify, I tried removing the "throws Exception" in Game's version of play and it printed "Playing Soccer...". Therefore, using the Soccer's version of play(). This is because of Game g = new Soccer();. But since Game's version of play() uses "throws Exception" and it is overridden in Soccer's version of play(), it will always be expected that it should be declared or handled by the caller?

My train of thought in this questions is:
1) Game g = new Soccer();
- use the version of play() method in Soccer class, but verify if it is also in Game. Check if it is a legal overridden/shadowed method.
2) g.play()
- should still use Soccer's version of play()
- In addition, (pls verify this) if the version from the super class throws an exception, it should consider the same behavior(being handled or thrown/)

Should same thinking applies to question: enthuware.ocajp.i.v8.2.1117? Also, please add tips in my train of thought if I missed out something. Thanks in advance.

other notes: "which method will be used depends on the actual class of the object that is referenced by the variable."

Re: About Question enthuware.ocajp.i.v7.2.998 :

Posted: Sat Feb 18, 2017 12:34 pm
by admin
Deleted User 3513 wrote: My train of thought in this questions is:
1) Game g = new Soccer();
- use the version of play() method in Soccer class, but verify if it is also in Game. Check if it is a legal overridden/shadowed method.
Your thought process is incorrect because you are mixing rules of compilation and rules of execution. When you say, "use the version of play() method in Soccer class", you need to understand that compiler doesn't know what object will the variable g point to at the time of execution. Thus, the compiler cannot decide this. It is the job of the JVM to decide which version of play method to execute.

When you say, "Check if it is a legal overridden/shadowed method. ". The compiler checks this when it compiles Soccer class. When the compiler encounters the statement Game g = new Soccer(); the only thing it needs to decide is whether it is ok for g to refer to an object of class Soccer. That's it. Since Soccer is a subclass of Game, compiler decides that yes, this assignment is valid.
2) g.play()
- should still use Soccer's version of play()
- In addition, (pls verify this) if the version from the super class throws an exception, it should consider the same behavior(being handled or thrown/)
Again, you are mixing compilation and execution. The compiler only knows that g will always refer to a Game. Whether it is actually an instance of Game class or an instance of a subclass of Game is irrelevant for the compiler. As far as the compiler is concerned g will point to a Game (it could be Soccer also, but compiler doesn't care because Soccer is also a Game). Compiler will take all decisions based on the declared type of g, which is Game.

Now, since the play method as declared in Game declares that it may throw a checked exception, the compiler has to check whether the call is wrapped in a try/catch block or not. If not, whether there is a throws clause in the encompassing method or not. If neither is there, then it will refuse to compile the code.
Should same thinking applies to question: enthuware.ocajp.i.v8.2.1117? Also, please add tips in my train of thought if I missed out something. Thanks in advance.
You can now apply the same concept to 2.1117 and see how that works.
other notes: "which method will be used depends on the actual class of the object that is referenced by the variable."
Correct.

Re: About Question enthuware.ocajp.i.v7.2.998 :

Posted: Mon Feb 17, 2020 12:15 pm
by Dani1515
the argumentation why it will not compile is very clear, but the question is very tricky imo. i focused on overriding, if it is everything ok with it and ignored throwed exception completely.
With an IDE this is very easy to figure out, the complier would complain about no catch block or throws clause...

Re: About Question enthuware.ocajp.i.v7.2.998 :

Posted: Thu Jun 03, 2021 8:54 am
by baichen7788
can we throws two exceptions in one method?

Re: About Question enthuware.ocajp.i.v7.2.998 :

Posted: Thu Jun 03, 2021 9:10 am
by admin
Not sure what you mean exactly but no, you can't do that.