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

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

Moderator: admin

Post Reply
Getjavahelp
Posts: 1
Joined: Sat Jul 08, 2017 4:13 pm
Contact:

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

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

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

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

Post by admin »

That is ok. An extra semicolon is a no-op. It is not a compilation error.
If you like our products and services, please help us by posting your review here.

sush23
Posts: 1
Joined: Wed Aug 16, 2017 9:27 am
Contact:

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

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

RRRRRR
Posts: 26
Joined: Sun Jul 23, 2017 2:13 am
Contact:

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

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

dvc1190
Posts: 15
Joined: Tue Feb 25, 2014 3:14 am
Contact:

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

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

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

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

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

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey »

Therefore, Powwow's wow() will be selected.
But Powwow does not have wow() method. Or it does?

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

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

Post by admin »

But its superclass does. So Powwow gets it from its superclass. This is not exactly like "inheriting" a superclass method but similar.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

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

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

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

Post by admin »

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

aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

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

Post 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) {
        

    }
}

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

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

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

aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

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

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

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

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

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

aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

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

Post by aPerson »

Ah, yes I am using an IDE. Thanks, read it now.

Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests