About Question enthuware.ocajp.i.v8.2.1494 :
Moderator: admin
-
- Posts: 1
- Joined: Sat Jul 08, 2017 4:13 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1494 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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.
-
- Posts: 1
- Joined: Wed Aug 16, 2017 9:27 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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.
-
- Posts: 26
- Joined: Sun Jul 23, 2017 2:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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.
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.
-
- Posts: 15
- Joined: Tue Feb 25, 2014 3:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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.
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.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
But Powwow does not have wow() method. Or it does?Therefore, Powwow's wow() will be selected.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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?Static methods of a interface are not inherited in the same way by an implementing class.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
Correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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) {
}
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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.
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.
-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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.
"...\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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
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
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.
-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1494 :
Ah, yes I am using an IDE. Thanks, read it now.
Who is online
Users browsing this forum: Google [Bot] and 121 guests