About Question enthuware.ocpjp.v8.2.1829 :

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

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1829 :

Post by schchen2000 »

Code: Select all

interface Measurement{

        public default int getLength(){ // <==== (1)

                return 0;

        }   

        public static int getBreadth(){

                return 0;

        }   

}

interface Size extends Measurement{

        public static final int UNIT = 100;

        public static int getLength(){ // <==== (2) 

                return 10; 

        }   

}

getLength method in Size is invalid because a default method cannot be overridden by a static method (or vice versa). 
The above block of code in green is what's given. I ran it as it's given and I've got
error: getLength() in Size clashes with getLength() in Measurement
public static int getLength(){
^
overriding method is static
1 error
as expected. When you said a default method cannot be overridden by a static method (or vice versa), that means a static method CANNOT be overridden by a default method as well if I can borrow your own words. I tried it and it compiled without any issue.

I've also tried to override a static method with an abstract method and this too compiles and ends without any issue.

What did you exactly meant when you said vice versa?

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

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by admin »

static method is never inherited, so it is not possible to override it. What code did you try?
-Paul.
If you like our products and services, please help us by posting your review here.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by schchen2000 »

admin wrote:static method is never inherited, so it is not possible to override it. What code did you try?
-Paul.
Hi Paul,

Many thanks. I've written some additional code to test this concept. Just want to double-check with you.

Static methods CANNOT be inherited from one interface to the next interface. Is that correct?

Static methods, however, CAN be inherited from one class to the next class. Is that correct?

Thanks.

Schmichael

PS

What code did you try?

I'll answer this in separate posts in this same thread.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by schchen2000 »

When you said a default method cannot be overridden by a static method (or vice versa), that means a static method CANNOT be overridden by a default method as well if I can borrow your own words. I tried it and it compiled without any issue.
This is what I meant by "I tried it and it compiled without any issue."

Please pay attention to the comments in the following code snippet.

Code: Select all

interface Measurement{

        public static int getLength(){ // <==== Original "default" is being replaced by "static" here. (1)

                return 0;

        }   

        public static int getBreadth(){

                return 0;

        }   

}

interface Size extends Measurement{

        public static final int UNIT = 100;

        public default int getLength(){ // <==== Original "static" is being replaced by "default" here. (2)

                return 10;

        }   

}
After this question was posed, I wrote some additional code to test the concept of inheriting a static method from one interface to the next interface.

And yes, static method CANNOT be inherited from one interface to another interface. Therefore, 2 getLength() methods right here are indeed UNRELATED.

Is that correct?

Everything I've said prior to this sentence still applies even if we make the getLength() method to be abstract in Size interface while the getLength() in the Measurement interface remains UNCHANGED.

Is that correct?

Thanks, Paul.

Schmichael

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

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by admin »

schchen2000 wrote:
admin wrote:static method is never inherited, so it is not possible to override it. What code did you try?
-Paul.
Static methods CANNOT be inherited from one interface to the next interface. Is that correct?
That is correct. For example, the following code will not compile:

Code: Select all

interface I1{
  public static void m1(){
     System.out.println("in I1.m1");
  }
}
interface I2 extends I1{
  public static void m2(){
     System.out.println("in I2.m2");
  }
}

public class TestClass {
  public static void main(String[] args) {
    I2.m1(); //I2 doesn't "get" method m1 from I1. 
  }
}
Static methods, however, CAN be inherited from one class to the next class. Is that correct?
This is a loaded question. static members are never "inherited" in the OO sense. The compiler does, however, allow you to access a static member of a super class using a subclass. For example, the following code works fine:

Code: Select all

class C1{
   public static void m1(){
     System.out.println("in C1.m1");
  }
}
class C2 extends C1{
  public static void m2(){
     System.out.println("in C2.m2");
  }
}

public class TestClass {

  public static void main(String[] args) {
    C2.m1(); //you can access C1's m1 using C2 but that doesn't mean C2 "inherits" m1.
  }
}
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by admin »

schchen2000 wrote:
When you said a default method cannot be overridden by a static method (or vice versa), that means a static method CANNOT be overridden by a default method as well if I can borrow your own words. I tried it and it compiled without any issue.
This is what I meant by "I tried it and it compiled without any issue."

Please pay attention to the comments in the following code snippet.

Code: Select all

interface Measurement{

        public static int getLength(){ // <==== Original "default" is being replaced by "static" here. (1)

                return 0;

        }   

        public static int getBreadth(){

                return 0;

        }   

}

interface Size extends Measurement{

        public static final int UNIT = 100;

        public default int getLength(){ // <==== Original "static" is being replaced by "default" here. (2)

                return 10;

        }   

}
After this question was posed, I wrote some additional code to test the concept of inheriting a static method from one interface to the next interface.

And yes, static method CANNOT be inherited from one interface to another interface. Therefore, 2 getLength() methods right here are indeed UNRELATED.

Is that correct?
That is correct.
Everything I've said prior to this sentence still applies even if we make the getLength() method to be abstract in Size interface while the getLength() in the Measurement interface remains UNCHANGED.

Is that correct?

Thanks, Paul.

Schmichael
That is correct.

Based on this discussion, I now feel that the explanation should be updated to make this point clear.

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

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by schchen2000 »

admin wrote:
schchen2000 wrote:
When you said a default method cannot be overridden by a static method (or vice versa), that means a static method CANNOT be overridden by a default method as well if I can borrow your own words. I tried it and it compiled without any issue.
This is what I meant by "I tried it and it compiled without any issue."

Please pay attention to the comments in the following code snippet.

Code: Select all

interface Measurement{

        public static int getLength(){ // <==== Original "default" is being replaced by "static" here. (1)

                return 0;

        }   

        public static int getBreadth(){

                return 0;

        }   

}

interface Size extends Measurement{

        public static final int UNIT = 100;

        public default int getLength(){ // <==== Original "static" is being replaced by "default" here. (2)

                return 10;

        }   

}
After this question was posed, I wrote some additional code to test the concept of inheriting a static method from one interface to the next interface.

And yes, static method CANNOT be inherited from one interface to another interface. Therefore, 2 getLength() methods right here are indeed UNRELATED.

Is that correct?
That is correct.
Everything I've said prior to this sentence still applies even if we make the getLength() method to be abstract in Size interface while the getLength() in the Measurement interface remains UNCHANGED.

Is that correct?

Thanks, Paul.

Schmichael
That is correct.

Based on this discussion, I now feel that the explanation should be updated to make this point clear.

HTH,
Paul.
Thanks a lot, Paul. I greatly appreciate it. I don't study for the test. I try to understand for life, if possible.

I'm the type of person who digs deep until I get it. I hope this is not too much for you. Once again, thank you very much, Paul.

Schmichael

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by lenalena »

Hello,

I have a question regarding the code posted above to illustrate access for a static method through a child interface.

The code that was posted to illustrate accessibility of of method m1() by interface I2 doesn't compile for me...

Gives compiler error "
Test.java:32: error: cannot find symbol
I2.m1(); //I2 doesn't "get" method m1 from I1.
^
symbol: method m1()
location: interface I2

If child interface can access parent interface static method, shouldn't compilation pass?

Thank you.

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

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by admin »

The code above does not claim to access parent interface's static method using child interface's name. It claims that a static method can be overridden by a default method in a child interface. It is true that the child interface can have a default method with the same signature as the parent interface's static method but it is not a case of override because static methods are not inherited at all.

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

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by lenalena »

admin wrote:The code above does not claim to access parent interface's static method using child interface's name. It claims that a static method can be overridden by a default method in a child interface. It is true that the child interface can have a default method with the same signature as the parent interface's static method but it is not a case of override because static methods are not inherited at all.

HTH,
Paul.
OK, so...
a child interface can override a static method with default but NOT default method with static.. And a child interface cannot access parent's static methods.
a child class does not "override" static methods because it doesn't technically inherit it, but can access it's ancestors' static methods.
A class cannot override an inherited (from an interface) default method with a static.

Is that correct? Am I missing any other default/static conditions?

Thank you

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

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by admin »

lenalena wrote: a child interface can override a static method with default
Please read my post above again. The word "override" is not applicable in this situation but yes, it can have a method with the same signature. This is not a case of "override" though.
lenalena wrote: but NOT default method with static..
Correct.

You need to understand that the word "override" has a specific meaning in Java and OOP (google). Merely having a method with the same signature in parent and sub class/interface does not necessarily involve "overriding".
lenalena wrote: And a child interface cannot access parent's static methods.
It can; using the name of the parent class/interface. If I2 extends I1, you can call I1.staticMethod(); from inside I2. But you cannot invoke it directly without the name (unlike in case of a class, where you can).
lenalena wrote: a child class does not "override" static methods because it doesn't technically inherit it, but can access it's ancestors' static methods.
Correct.
lenalena wrote: A class cannot override an inherited (from an interface) default method with a static.
Correct.

You should try out some test programs to verify and to make sure you remember the rules. There are questions in the exam on this type of thing.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by lenalena »

Thanks, Paul,

I did write test programs, and I'm just trying to generalize and confirm what they showed.

Thank you for taking the time to go through it.

Also, a class cannot invoke the implemented interface's static method directly, has to call it through interface's name. So it's not about who does the calling, it's about who declares the static method. Interface's static methods cannot be accessed directly, while a class' can...

On the side note, is there a logical reason why a class' static method can be accessed by children while an interface's cannot? Is it because there is no implied "super" reference to an implemented/extended interface?

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

Re: About Question enthuware.ocpjp.v8.2.1829 :

Post by admin »

lenalena wrote: On the side note, is there a logical reason why a class' static method can be accessed by children while an interface's cannot? Is it because there is no implied "super" reference to an implemented/extended interface?
I haven't come across any valid reason for why a class' static method can be accessed by children directly. It should not have been able to and is a design flaw in the language, IMHO. I just made this argument here: https://coderanch.com/t/680085/certific ... ce#3189912

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

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 31 guests