Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Sat Jul 08, 2017 4:20 pm
by Getjavahelp
Hello,

In OCAJP test 6-question 52, It should be compile time error as there is a ; at the end of wow () method which is static in Pow interface.

But evalute showing as first option. Can you please correct me if my evaluation is wrong.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Sat Jul 08, 2017 11:18 pm
by admin
That is ok. An extra semicolon is a no-op. It is not a compilation error.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Wed Aug 16, 2017 9:31 am
by sush23
I am not able to clearly understand the answer, can static methods of super class be called from child class. My understanding is that static methods can be only hidden and not inherited/overridden? Please clarify.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Wed Aug 16, 2017 10:49 am
by RRRRRR
Yes it is method hiding and not method overriding. Static methods are not inherited but are hidden in child class->If we declare same method with same signature which is present in base class in derived class also.

Like->

class A
{
static void asus()
{
System.out.println("A's Asus");
}

}
class B extends A
{
static void asus() //It is method hiding and not method overriding!!!
{
System.out.println("B's Asus");
}

}

class main
{
public static void main(String[] args)
{
A a=new A();
B b=new B();
A a1=new B(); //See here

a.asus(); //Output->A's Asus
b.asus(); //Output->B's Asus
a1.asus(); //Output->A's Asus //See here output is A's Asus even object is of B. because it is method hiding thats why it sees only reference type and not object type.
}
}


Hope it would help. :)

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Tue Sep 12, 2017 5:44 pm
by dvc1190
I don't get how it's not ambiguous as to which wow method is called. Is there a rule or something that the superclass's method takes precedence over the Interface?

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Tue Sep 12, 2017 10:24 pm
by admin
Yes, static members (i.e. methods as well as variables) and instance variables are selected based on the declared type of the reference variable. (Unlike instance methods, which are selected based on the actual class of the object referred to by the variable).
Here, the declared type of the variable f is Powwow. Therefore, Powwow's wow() will be selected.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Sun Oct 29, 2017 3:06 pm
by Sergey
Therefore, Powwow's wow() will be selected.
But Powwow does not have wow() method. Or it does?

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Sun Oct 29, 2017 8:33 pm
by admin
But its superclass does. So Powwow gets it from its superclass. This is not exactly like "inheriting" a superclass method but similar.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Wed Jan 09, 2019 5:14 pm
by crazymind
Static methods of a interface are not inherited in the same way by an implementing class.
But I find that "Static methods in interfaces are never inherited". Is this true? Basically, the subclass only inherits the static method from its parent class in this question. Is it correct?

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Wed Jan 09, 2019 7:36 pm
by admin
Correct.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Mon Jan 30, 2023 8:12 am
by aPerson
Hi, do you have an explanation why the following is fine for the compiler (and crashes when run), while the compiler complains if I remove "static" making the wow() in Wow a packageprivate instance-method? (I understand why it crashes in the first case and why it complains in the second case, just not the part that the compiler thinks it's fine). Is it because TestClass inherits Wow's static method in a different way than it would inherit an instance-method, a way where it is treated as if it were in TestClass first at runtime? I saw you mentioned something along these lines above.

Code: Select all

interface Pow {
    default void wow() {
        System.out.println("In Pow.wow");
    }
}

 class Wow {
    static   void wow() {
        System.out.println("In Wow.wow");
    }
}

public class TestClass extends Wow implements Pow {
    public static void main(String[] args) {
        

    }
}

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Mon Jan 30, 2023 10:02 am
by admin
1. What do you mean by "crashes"?

2. Please post the code and the exact error message that you get while compilation and while execution so that we can provide some explanation that addresses you doubt.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Mon Jan 30, 2023 11:23 am
by aPerson
1. Hi, ok maybe I was being unclear. By "crashes" I mean it won't run because wow() in Wow is static and therefore can't override the wow() in Pow. Error message is:

"...\TestClass.java:15:8
java: wow() in com.codewithoy.Wow cannot implement wow() in com.codewithoy.Pow
overriding method is static"

This is completely understandable. My question is why it compiles fine? I'm wondering because when removing the "static" word from Wow it won't compile (because of weaker access-privileges, which is completely understandable too). The point was just that in the code I've written out in the first question, the compiler doesn't notice the incompatibility (stemming from trying to override a default with a static method), but when "static" is removed from Wow it does see that the methods are incompatible (because of weaker access-privileges in that case). Why does the compiler not notice the default/static incompatibility when the code is written as above?

2. Exact code is given in the first question. Errormessage when this code is run is given in "1.". It is not the errormessage that confuses me, it is the fact that the compiler thinks the code is fine.

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Mon Jan 30, 2023 11:33 am
by admin
I am confused. You are posting a compilation error and are then asking, "My question is why it compiles fine?"

As the error message that you posted clearly shows, it does not compile. So why are you thinking that it compiles? Compiler does not think that the code is fine. It generates an error message (the one that you posted).

I suspect you are using an IDE. Please see this: https://enthuware.com/oca-ocp-java-cert ... cation-ide

Re: About Question enthuware.ocajp.i.v8.2.1494 :

Posted: Mon Jan 30, 2023 11:49 am
by aPerson
Ah, yes I am using an IDE. Thanks, read it now.