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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

TheSarjo
Posts: 15
Joined: Fri Jul 12, 2013 12:34 pm
Contact:

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

Post by TheSarjo »

Ok! got it
thank you!

Deleted User 3513

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

Post 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."

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Dani1515
Posts: 9
Joined: Mon Feb 17, 2020 12:10 pm
Contact:

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

Post 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...

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

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

Post by baichen7788 »

can we throws two exceptions in one method?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Not sure what you mean exactly but no, you can't do that.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 38 guests