Page 1 of 1

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

Posted: Thu May 31, 2012 9:34 pm
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.

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

Posted: Fri Jun 01, 2012 12:54 pm
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.

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

Posted: Fri Jun 01, 2012 3:53 pm
by Jix
ok i get it now, a tricky question :)

thanks

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

Posted: Fri Apr 11, 2014 9:15 am
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.

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

Posted: Fri Apr 11, 2014 9:23 am
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.

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

Posted: Mon Jun 02, 2014 9:07 am
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.

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

Posted: Mon Jun 02, 2014 8:06 pm
by admin
You can find all the rules in Java Language Specification :)

HTH,
Paul.

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

Posted: Thu Sep 04, 2014 5:01 pm
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.

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

Posted: Thu Sep 17, 2015 6:48 am
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");
    }
}

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

Posted: Thu Sep 17, 2015 9:33 pm
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.

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

Posted: Fri Sep 18, 2015 3:14 am
by heleneshaikh
Thank you very much for the clear explanation!

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

Posted: Sun Feb 25, 2018 10:24 am
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.

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

Posted: Sun Feb 25, 2018 10:40 am
by admin
What error message did you get when you tried to compile it?

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

Posted: Wed Nov 06, 2019 5:44 pm
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?

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

Posted: Wed Nov 06, 2019 8:52 pm
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.

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

Posted: Sun Jun 28, 2020 9:59 am
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: