Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Questions on the relationship between static and non-static methods

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following code has been taken from one of Enthuware's mock exams.


As you can see, the methods wow() in both interface Pow and class Wow are static. The code compiles successfully.

When we change Wow's method into an instance method, the code still compiles.


However, let's say we then change's Pow's method into an abstract method:


This results in the following error:

Powwow.java:16: error: wow() in Wow cannot implement wow() in Pow
public class Powwow extends Wow implements Pow {
      ^
 overriding method is static
1 error


1) The error states that Wow's (the abstract class) method is trying to implement Pow's (the interface) method. I don't understand where this is coming from though. Wow does not implement the interface Pow, so aren't these two methods separate? I did notice that if I comment out f.wow(), the code does compile. It obviously has something to do with the way the method is called, but I can't figure out what it is.

2) So let's say that Wow's method is trying to override Pow's method (which is true, apparently). Why can't a static method override a non-static method, while a non-static method can override a static method (as demonstrated with the first code edit in this post)?

Next, let's make both methods non-static.

Attempting to compile this code produces the following error:

Powwow.java:13: error: wow() in Wow cannot implement wow() in Pow
public class Powwow extends Wow implements Pow {
      ^
 attempting to assign weaker access privileges; was public
1 error



3) Again, it seems that Wow's method is trying to override Pow's method, since it's access modifier has now gone from public to default, which isn't allowed. I suppose this compilation failure pertains to the same issue as brought up in my first question, right?

Thanks again for your support!

Regards,
Shane
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
1) The error states that Wow's (the abstract class) method is trying to implement Pow's (the interface) method. I don't understand where this is coming from though. Wow does not implement the interface Pow, so aren't these two methods separate? I did notice that if I comment out f.wow(), the code does compile. It obviously has something to do with the way the method is called, but I can't figure out what it is.



The Wow class does *not* implement the Pow interface, however, the Powwow class must implement the Pow interface (as it is declared to do so). Unfortunately, according to section 8.4.8.1 of the JLS, this is not allowed...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.1

as doing so will have an instance method trying to override a static method.

Henry
 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
3) Again, it seems that Wow's method is trying to override Pow's method, since it's access modifier has now gone from public to default, which isn't allowed. I suppose this compilation failure pertains to the same issue as brought up in my first question, right?



The Powwow class must implement the Pow interface (as it is declared to do so). The Pow interface has the wow() method with public access levels, since all interface methods are public.

The Powwow class does *not* implement the wow() method; instead it inherits it from the Wow class, which does not have a public access level, as required by the Pow interface. Hence, you get the error message from the compiler.


... and ... I am not sure what you are trying to say with point 2. Can you elaborate?

Henry
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Shane Jensen wrote:
1) The error states that Wow's (the abstract class) method is trying to implement Pow's (the interface) method. I don't understand where this is coming from though. Wow does not implement the interface Pow, so aren't these two methods separate? I did notice that if I comment out f.wow(), the code does compile. It obviously has something to do with the way the method is called, but I can't figure out what it is.



The Wow class does *not* implement the Pow interface, however, the Powwow class must implement the Pow interface (as it is declared to do so). Unfortunately, according to section 8.4.8.1 of the JLS, this is not allowed...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.1

as doing so will have an instance method trying to override a static method.

Henry


Thanks, Henry! Your explanation cleared things up for me.

Regarding my second question: I don't understand why a static method is not allowed to override a non-static method.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm not sure whether a non-static method can even override a static method. After the first code change I made, is the static method from class Wow even overriding the non-static method from interface Pow? If not, what is happening? And is it possible for a non-static method to override a static method?
 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
Regarding my second question: I don't understand why a static method is not allowed to override a non-static method.



Overriding does not apply to static methods. It is for instance methods, that are accessible. So... the answer to the question is, no, overridding does not apply for static methods. Period.

But that is not what you are asking here. You are asking whether it compiles or not -- or at least, that is how I answered it.

Henry
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Shane Jensen wrote:
Regarding my second question: I don't understand why a static method is not allowed to override a non-static method.



Overriding does not apply to static methods. It is for instance methods, that are accessible. So... the answer to the question is, no, overridding does not apply for static methods. Period.

But that is not what you are asking here. You are asking whether it compiles or not -- or at least, that is how I answered it.

Henry


True. So then my question would be: why does the following error occur? The method in the class is static, while the method in the interface is non-static. But why is that a problem?

Powwow.java:16: error: wow() in Wow cannot implement wow() in Pow
public class Powwow extends Wow implements Pow {
      ^
 overriding method is static
1 error

 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
True. So then my question would be: why does the following error occur? The method in the class is static, while the method in the interface is non-static. But why is that a problem?

Powwow.java:16: error: wow() in Wow cannot implement wow() in Pow
public class Powwow extends Wow implements Pow {
      ^
 overriding method is static
1 error



I already answered this above.

Henry
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right.. I guess I haven't been exactly sharp today yet.   Got it now, finally. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!