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

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

Moderator: admin

Post Reply
Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

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

Post by Jix »

Why do I have to comment Line 1? isn't this shadowing and shadowing doesn't cause a compile time error?

I chose option 2 and option 4 that states that only option 2 is correct.

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

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

Post by admin »

Because this is a Java language rule that you cannot have a static and non-static method with the same signature in a class hierarchy.
So if you uncomment line 1 and 2, then it violates the above rule.

If you only uncomment line 1, that is fine. (That is why option 1 is incorrect)
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

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

Post by Jix »

ok i get it now, a tricky question :)

thanks

kwetal
Posts: 5
Joined: Thu Apr 03, 2014 3:50 am
Contact:

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

Post by kwetal »

Selecting option 4 (Only Option 2 is correct) or option 5 (Only Option 3 is correct) creates a contradiction: if option 4 were correct then its assertion (Only Option 2 is correct) is not true any more, because also option 4 is correct. The same goes for option 5.

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

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

Post by admin »

kwetal wrote:Selecting option 4 (Only Option 2 is correct) or option 5 (Only Option 3 is correct) creates a contradiction: if option 4 were correct then its assertion (Only Option 2 is correct) is not true any more, because also option 4 is correct. The same goes for option 5.
You are logically correct but I feel the intention of the option is conveyed correctly so I am going to leave these options as they are.
thanks for your feedback though.
-Paul.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post by vchhang »

Hi Paul,

It appears that there are lots of these sneaky java rules. I thought having either line 1 or line 2 is ok but not both due to shadowing since static members are never inherit.

You stated earlier:
"Because this is a Java language rule that you cannot have a static and non-static method with the same signature in a class hierarchy."

Where do you find all of this? So many contradictory/exceptions to various rules in Java.

Thanks in advance.

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

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

Post by admin »

You can find all the rules in Java Language Specification :)

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

dklein604
Posts: 1
Joined: Thu Sep 04, 2014 4:32 pm
Contact:

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

Post by dklein604 »

The wording on the answers is a little confusing. Maybe you should make option E 'Only option C is correct' (same goes with D), because you letter them but refer to them as numbers.. Also as mentioned above neither of those answers make any logical sense.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

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

Post by heleneshaikh »

Hello, I'm trying to understand the logic behind the code below. Why do we look at the actual class object A refers to (that is B) in line 1, while we look at A in lines 2 and 3? What's the logic behind it? When and why do we have to look at the actual object (RHS) that is being referred to vs the reference variable on the left? Many thanks in advance, this has been bothering me for quite a while.

Code: Select all

public class Animals {
    public static void main(String[] args) throws IOException {
       A a = new B();                       //Line 1: B constructor called, then the superclass' constructor
       a.test();                             //Line 2: A's method called, which is actually overridden by B
       System.out.println(a.a);        //Line 3: A's instance variable is called.
    }
}

class A {
    int a = 4;
    public A() {
        System.out.println("A constructor");
    }
    
    public void test() {
        System.out.println("A test");
    }
}

class B extends A {
    int a = 40;
    public B() {
        System.out.println("B constructor");
    }
    @Override
    public void test() {
        System.out.println("B test");
    }
}

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

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

Post by admin »

There are three things that you need to know:

1. Compiler does not know the actual object to which a reference points because objects are created at runtime and compiler doesn't execute any code. Compiler only knows the declared type of a reference. So, while compilation, a compiler will always verify if the method that you are trying to call on a variable is possible by checking if that method is declared in the class of the variable (not object).

2. At the time of execution, the JVM knows the object to which a reference points. Therefore, it binds the method call to the actual class of the object. This is called polymorphism.

3. Calls to static methods and access to variables is never polymorphic. So the compiler binds such calls and access to the declared class of the reference (instead of the class of actual object) at compile time itself.

Based on the above 3 points it is easy to see what you need to see when.

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

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

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

Post by heleneshaikh »

Thank you very much for the clear explanation!

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

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

Post by Meghana »

"class B will not compile if line 1 and 2 are both uncommented" is one of the right choices.

But why? There can be a class with an empty body right?

Thank you.

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

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

Post by admin »

What error message did you get when you tried to compile it?
If you like our products and services, please help us by posting your review here.

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

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

Post by DazedTurtle »

I remember in the foundation test that
the field being accessed depends on the class of the variable, not of the actual object
But in your explanation for this question, you mention

Code: Select all

A a  = new B();
System.out.println(a.i)  //will print 10 instead of 20
a.m1();  //will call A's m1
a.m2();  //will call B's m2 as m2() is not static and so overrides A's m2()
So does this mean that fields and static methods accessed depend on the class of the variable, but non-static methods accessed depend on the object?

Also, based on the fact that you don't do it, I'm guessing the actual exam does not warn you if you go to the next question without selecting the correct number of answers?

Because I remember choosing both correct answers for this question, but I must have misclicked, and it only shows that I selected one of them. This isn't the first time I've missed a question because I didn't select enough answers, too.

On a related note, I'm assuming the exam doesn't do partial credit, either? I'd have to get all answers correct, or else it's wrong?

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

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

Post by admin »

>So does this mean that fields and static methods accessed depend on the class of the variable, but non-static methods accessed depend on the object?
Yes, access to fields and static methods depend on the class of the reference. The explanation about the field that you have quoted is to explain the issue with that specific question/option. We will enhance the explanation to make it clear.

>Also, based on the fact that you don't do it, I'm guessing the actual exam does not warn you if you go to the next question without selecting the correct number of answers?
Yes, it will not warn you if you don't select the required number of options for a question but on the review screen (the real exam also has a review screen), where you see the list of all the questions, you will see such questions marked as "Incomplete".

>On a related note, I'm assuming the exam doesn't do partial credit, either? I'd have to get all answers correct, or else it's wrong?
Correct. No partial credit. All or nothing.
If you like our products and services, please help us by posting your review here.

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

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

Post by Sieusc »

admin wrote:
Fri Apr 11, 2014 9:23 am
kwetal wrote:Selecting option 4 (Only Option 2 is correct) or option 5 (Only Option 3 is correct) creates a contradiction: if option 4 were correct then its assertion (Only Option 2 is correct) is not true any more, because also option 4 is correct. The same goes for option 5.
You are logically correct but I feel the intention of the option is conveyed correctly so I am going to leave these options as they are.
thanks for your feedback though.
-Paul.
:lol:

This actually is a funny remark in itself, and indeed the intention of the question and it's options is conveyed loud and clear. I guess this is what the laser focus required for passing the OCA does to a man/woman :geek:

Post Reply

Who is online

Users browsing this forum: No registered users and 35 guests