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

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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

The option:
A a = new B();
a.m();

If it did compile, wouldn't it be invoking B's m()? Is the correct answer of:
((B) a).m();

to satisfy the compiler, but in reality B's m() would be invoked anyway?

I thought I understood how the polymorphism worked, but I find this a new twist that I don't understand.

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

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

Post by admin »

ETS User wrote:The option:
A a = new B();
a.m();

If it did compile, wouldn't it be invoking B's m()? Is the correct answer of:
((B) a).m();

to satisfy the compiler, but in reality B's m() would be invoked anyway?

I thought I understood how the polymorphism worked, but I find this a new twist that I don't understand.
Yes, even without the cast B's m() will be invoked. But that happens at runtime. The issue here is what happens at compile time. Without the cast the compiler will check for checked exception using the method signature of A's m1 because the variable 'a' is declared of class A. Since A's m1's signature throws a checked exception and since the call to a.m1() is not wrapped in try catch, the compiler will complain.

When you cast a to B using ((B)a), you are telling the compiler to check the code using B's m1's signature. Since B's m1 doesn't declare any checked exception, the compiler is happy even if the call ((B)a).m1() is not wrapped in a try-catch.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

yolobang
Posts: 2
Joined: Mon Sep 30, 2013 1:50 pm
Contact:

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

Post by yolobang »

I answered none of these since the classes are in different files and are not in the same package. Thinking about it now, after I made this account and typed the previous sentence, I realize that the classes are public and that you do not need a package declaration then.

Tomorrow exam. Wish me luck. Top score after doing all the exams was only 65% so far...

yolobang
Posts: 2
Joined: Mon Sep 30, 2013 1:50 pm
Contact:

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

Post by yolobang »

Passed with 86%. Thank you very much Enthuware!

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

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

Post by admin »

Great job! Congratulations!!
-Paul.
If you like our products and services, please help us by posting your review here.

Sergio
Posts: 7
Joined: Wed Mar 23, 2016 1:39 am
Contact:

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

Post by Sergio »

Please check this:
The compiler does not complain at all about the cast https://ideone.com/t2avOt and I don't need to catch or declare the throws of the Exception

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

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

Post by admin »

Correct.
If you like our products and services, please help us by posting your review here.

Sergio
Posts: 7
Joined: Wed Mar 23, 2016 1:39 am
Contact:

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

Post by Sergio »

so it is a mistake in the response.

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

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

Post by admin »

Could you please be specific as to what you think is a mistake? Attached is the screen shot of the explanation that I see and it says that same thing what you are saying, i.e. try/catch block is not needed for the code given in this option.
Paul.
Attachments
2.1167.png
2.1167.png (26.16 KiB) Viewed 12505 times
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Hi!

I thought that to use classes in different files we should import that ones or fully write their names.
I am comfused now :(

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

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

Post by admin »

You have to assume appropriate import statements. Oracle has made it clear in their exam instructions.
Assume the following:
Missing package and import statements: If sample code do not include package or import statements, and the question does not explicitly refer to these missing statements, then assume that all sample code is in the same package, or import statements exist to support them.
No file or directory path names for classes: If a question does not state the file names or directory locations of classes, then assume one of the following, whichever will enable the code to compile and run:
All classes are in one file
Each class is contained in a separate file, and all files are in one directory
Unintended line breaks: Sample code might have unintended line breaks. If you see a line of code that looks like it has wrapped, and this creates a situation where the wrapping is significant (for example, a quoted String literal has wrapped), assume that the wrapping is an extension of the same line, and the line does not contain a hard carriage return that would cause a compilation failure.
Code fragments: A code fragment is a small section of source code that is presented without its context. Assume that all necessary supporting code exists and that the supporting environment fully supports the correct compilation and execution of the code shown and its omitted environment.
Descriptive comments: Take descriptive comments, such as "setter and getters go here," at face value. Assume that correct code exists, compiles, and runs successfully to create the described effect.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Thank you very much for the answer, I was worried :)

li_hanglin@bah.com
Posts: 6
Joined: Mon Jan 28, 2019 4:18 pm
Contact:

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

Post by li_hanglin@bah.com »

For the second answer option, since the object is of class B, why doesn't the overridden method gets call?

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

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

Post by admin »

Overridden method is indeed called. The problem in the second option is about compilation. Compiler doesn't know that a points to an object of class B. JVM knows. So the compiler only validates the method call based on the declared type of the reference.

Please go through a good book about this concept before attempting mock exams. This is very important.
If you like our products and services, please help us by posting your review here.

exc35510n
Posts: 2
Joined: Sun Oct 20, 2019 12:16 pm
Contact:

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

Post by exc35510n »

Does the fact that m() method in class A is protected also prevents us from calling a.m()?
The problem is that I don't understand if comments like //in file A.java, //in file B.java etc. imply that those classes are in different packages or not, because if they are then code wouldn't compile even if throws SomeException is removed from class A. Am I correct?

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

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

Post by admin »

If the comment says the code is in file A.java then all it means is that the code that follows the comment is written in file A.java. This has nothing to do with package.

Package of a class is determined by the package statement. You need check if the code in the file starts with a package statement to determine the package to which the class belongs.
If you like our products and services, please help us by posting your review here.

exc35510n
Posts: 2
Joined: Sun Oct 20, 2019 12:16 pm
Contact:

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

Post by exc35510n »

But the package statement is not included in given code snippet. Does that mean that I have to assume that all files are in the same package? So if classes were intended to be in different packages then we would have comments like like //in file /root/com/foo/B.java and //in file /root/com/bar/B.java or we would have actual package statements in the code. Is that correct?

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

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

Post by admin »

You don't have to assume it. It is a known fact that if there is no package statement in the file then that means the class belongs to the unnamed aka the default package.

No, if classes were intended to be in different packages then we would NOT have comments like //in file /root/com/foo/B.java. We would have package statements given in the code listing, something like, package a.b.c; or whatever is the package name.
If you like our products and services, please help us by posting your review here.

jeff_kola
Posts: 3
Joined: Mon Dec 07, 2020 1:34 pm
Contact:

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

Post by jeff_kola »

Must all overriding explicitly declare the exception? I always thought exceptions were passed down, meaning that an exception declared in the superclass's original method is implicitly present in the subclass's overriding method.

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

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

Post by admin »

No, an overriding method is allowed to declare a subset of exceptions declared in the throws clause of the overridden method. zero exceptions is a valid subset and so, it is ok for the overriding method to not declare any exception in its throws clause. Further, the overriding method can also declared the subclass exceptions of the exceptions declared in the overridden method.

You might want to go through section 12.4.2 of OCP Java 11 Fundamentals by Hanumant Deshmukh ( https://amzn.to/2PucBeT ) . It explains the reasoning behind it.

>I always thought exceptions were passed down, meaning that an exception declared in the superclass's original method is implicitly present in the subclass's overriding method.
I am not sure where you read it and what it means. There is no such thing.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests