• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static method in Interface  RSS feed

 
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
My understanding of a static method is that the method can be called directly on its class. And that the method can also be called on the instance of its class.


So, what does it mean when a static method is defined inside an  Interface?

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biniman Idugboe wrote:
So, what does it mean when a static method is defined inside an  Interface?


To call a static method of an interface, you need to specify the interface only. I do *not* believe that you can call it with an instance that IS-A that interface, or by specifying a class that implements that interface.

Henry
 
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods in interfaces are tied to interfaces and do not get inherited in any class which implements the interface. As such that static methods cannot be extended by any class and you must supply the method body and must be called by referencing the interface.
Instead of:

you have
 
Biniman Idugboe
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thank both respondents.  The code was modified as follows:


When the default modifier was not part of the definition, there this "error: ExampleXX is not abstract and does not override abstract method addTwoNumbers() in AnotherExampleXX". What exactly does the word default add to the method definition? Does it mean that only static or default methods can ever be in an interface?
 
Biniman Idugboe
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, replace System.out.println(sumTwoNumbers());  with System.out.println(new ExampleXX().addTwoNumbers());

Somewhat embarrassing! I could not find the means to edit my post. Apology.
 
Pete Letkeman
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biniman Idugboe wrote:System.out.println(doSomethingElse());  //not correct.  Why?  Does the class dissociates itself from a static method that belongs to an interface?

Correct, you need to prefix the static method from an interface with the name of the interface.
Or, if you are dealing with a static method in a class then you need to prefix the name of the static method with the name of the interface.
Biniman Idugboe wrote:What exactly does the word default add to the method definition? Does it mean that only static or default methods can ever be in an interface?

You can have many different methods of different types (static, default, 'standard') in interfaces, as my sample below shows.
It's my understanding that one of the big reasons for default methods was to provide some new life to existing packages/classes, specially collection classes, by providing backwards capabilities without requiring massive rewrites.

Some more information about default and static methods:
  • You cannot have both a default method and a static method in the same interface with the same name. It's either static method or default method or a standard method.
  • Static methods and default methods need method bodies.
  • Static methods are not inhered in the classes or the interfaces which implement or extend them.
  • Default methods are inhered in the classes or the interfaces which implement or extend them.

  • You can have a method defined in an interface, which is not default and not static, but the first concrete class which implements that interface must provide a method body.

    Java does some interesting things behind the scene which you may not know about, for instance the follow interfaces with methods are the same according to Java:

    MyInterface could have any number of static or default methods as well.

    To implement MyInterface you would need something like:


    ------------
    Editing your post after the fact is not enabled by default. You need to get enough cows https://coderanch.com/wiki/659926/Ranch-Cows to do this. Please be aware that this functionality is subject to change.
    ------------
     
    Biniman Idugboe
    Ranch Hand
    Posts: 42
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.  But, bear with me as I have some more questions on this issue.


    1.  If Java regards all the above interface methods as being the same, which of the them does Java defaults to?

    2.  Some have public modifier and some do not have it. What is the difference?  I remember that in VB, the default modifier is Public.

    3.  I have also come across phrases such as Functional interface. At what point does an interface becomes a Functional interface. Is the example given above (myInterface) a functional interface?

    4.  When you say that a default or a static method is not inherited in the class or the interface that implemented it, I suppose you mean, in my own words, the class or interface does not claim ownership of a default method or a static method that belongs to another class or interface.  Correct?

    Your explanation regarding editing a post is noted.
     
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Biniman Idugboe wrote:Thank you.  But, bear with me as I have some more questions on this issue.
    1.  If Java regards all the above interface methods as being the same, which of the them does Java defaults to?
    It is more a question of which of those will you write. Since those methods are override‑equivalent, a class or interface containing all those methods will fail to compile. You only write one of them; the custom is to omit the modifiers and write void myMethod();
    2.  Some have public modifier and some do not have it. What is the difference?  I remember that in VB, the default modifier is Public.
    VB is a different language; forget all about it. Look in the Java® Language Specification (=JLS), where you will find that interface methods are implicitly public.
    That JLS section wrote:Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.
    So there is no difference.
    javac MyInterface.java
    MyInterface.java:3: error: modifier private not allowed here
       private void foo();
                    ^
    MyInterface.java:4: error: modifier protected not allowed here
       protected void bar();
                      ^
    2 errors
    3.  I have also come across phrases such as Functional interface. At what point does an interface becomes a Functional interface. Is the example given above (myInterface) a functional interface?
    You will find a definition of functional interface in the same part of the JLS:-
    A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. . . .
    It has one method and one method only which must be implemented in non‑abstract implementing classes (=abstract method). That doesn't include static methods, nor default methods, nor any of the nine public methods of java.lang.Object. You may find the details in the @FunctionalInterface annotation helpful, too. So Comparator is a functional interface; if you try to create a λ with it, the javac tool can work out that you need to implement the compare method and not one of the other methods. If you look at its methods and click the tab labelled abstract methods, only two methods will remain visible and the other twelve will be hidden. But you will see that one of the methods is equivalent to a method from Object, and it even says in the documentation that you do not need to implement it. It may be easier to visualise in the Java7 version, but both versions say the same thing. You will also see that the Java8 version is tagged with the @FunctionalInterface annotation.
    Now, if a functional interface is one with one abstract method only, you shou‍ld be able to work out whether what you showed is a functional interface or not, and you can try adding the annotation to it and see whether the compiler accepts it.
    4.  When you say that a default or a static method is not inherited in the class or the interface that implemented it, I suppose you mean, in my own words, the class or interface does not claim ownership of a default method or a static method that belongs to another class or interface.  Correct? . . .
    What does the JLS say? I think you will find that in this section. I think it says that all non‑private members of superinterfaces are inherited.After changing line 19 to read MyInterface.baz();
    java MyClass
    baz method
    bar method
    So you can see that the static method is not accessible in the implementing class, but the default method is.
     
    Biniman Idugboe
    Ranch Hand
    Posts: 42
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Again, thank you. There is certainly more to these things than meets the eye.  And these responses are very insightful. Now, I came upon the following statement in the ducumentation:
    Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

    Could you expatiate on this, perhaps, give some examples?
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I suggest you might do well to read this Java™ Tutorials section, and see the difference between a λ expression and an anonymous class.
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Pete Letkeman wrote:. . . one of the big reasons for default methods was to provide some new life to existing packages/classes, specially collection classes,  . . .
    The best way to make it possible to create a Stream from every List would be to add a stream() method to the List interface. Unfortunately, adding an abstract method to an interface would break all implementing code which didn't already have an implementation. Adding such a method to an abstract superclass wouldn't work because
  • 1: Some List implementations might not extend that class.
  • 2: The method wouldn't be available in the List interface.
  • So they decided to reuse the old default keyword, and the stream() method becomes available to old Lists created in Java7− if the code is used on a Java8 runtime. Now of course, they have the opportunity to create more default methods, e.g. List#sort.
    A static method is good for factory methods, e.g. this method.
     
    Biniman Idugboe
    Ranch Hand
    Posts: 42
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot.  I think I should be able to carry on now; as far as this topic is concerned.
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Priya Arrora, welcome to the Ranch
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!