1

I am having a problem with calling a default method from an interface. Here is my code:

Interface:

public interface Pozdrav {

    public static void stat(){
        System.out.println("Ja sam staticni metod");
    }

    default void osn(){
        System.out.println("Ja sam osnovni metod");
    }
}

Main class:

public class KonkretniPozdrav  implements Pozdrav{

    public static void main(String[] args) {

        Pozdrav.stat();
    }
}

Now, I need to call the default method Pozdrav.osn(); but when I do that, I receive this error:

Error:(8, 16) java: non-static method osn() cannot be referenced from a static context.

How do I call this default method?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142

4 Answers4

4

You need a concrete instance to invoke the method on. If you have not yet created a concrete type, you could do so anonymously. Like,

new Pozdrav() {
}.osn();

Outputs

Ja sam osnovni metod
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
4
new KonkretniPozdrav().osn();

In order to call osn, an instance of Pozdrav is required.

A default method (instance member) doesn't mean a static method (class member). It suggests that the method will have the body in an interface. default methods provide the default behaviour for implementations. That's why you didn't have to implement osn in KonkretniPozdrav.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • I know, I used it like I did for the stat method. But it didn't work – Milan Maxa Maksimović Mar 19 '18 at 20:33
  • @MilanMaxaMaksimović **`new KonkretniPozdrav()`**. – Elliott Frisch Mar 19 '18 at 20:36
  • @MilanMaxaMaksimović, `osn` is an *instance* member, so it can't be called from a static context. Does it make sense? – Andrew Tobilko Mar 19 '18 at 20:53
  • Yes, thank you. I am still new to Java, and this instances are still a bit complicated for me. But you cleared it out for me. – Milan Maxa Maksimović Mar 19 '18 at 21:04
  • 1
    @MilanMaxaMaksimović When you didn’t understand object instances, you are starting at the wrong end when playing around with interfaces and default methods. You will miss the entire purpose of interfaces when you didn’t learn about classes and objects before. Have a look at [the tutorials](https://docs.oracle.com/javase/tutorial/index.html). – Holger Mar 20 '18 at 11:54
2

You need an instance of Pozdrav to call an instance method on it. For example:

new Pozdrav() {}.osn();
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Thank you, it works. Can you explain to me why this is the way for a default method to be called? I am eager to learn. – Milan Maxa Maksimović Mar 19 '18 at 20:35
  • As stated in the first sentence: you need an instance of a class to call an instance method on it. A default method is an instance method; a static method is not an instance method. – Andy Turner Mar 19 '18 at 20:42
1

To call non static methods, you should create a instance of the class using the keyword new, example: KonkretniPozdrav pozdrav = new KonkretniPozdrav();. To call static methods, you don't need a instance. Just call using CLASS.Method().

Your main class would appear like it.

public class KonkretniPozdrav implements Pozdrav{

    public static void main(String[] args) {

        Pozdrav.stat();
        KonkretniPozdrav konkretnipozdrav = new KonkretniPozdrav();
        konkretnipozdrav.osn();
    }
}

A consideration for your code is that interfaces shouldn't have code implemented, except in static methods and default methods, which are allowed code in the body. Interfaces are contracts that classes that implement the interface should comply/obey. Normally is a convention start a interface with the letter I to indicate interface, example: IPozdrav. Here a document about Java interfaces.

Maybe, you would look at the difference between Abstract class vs Interfaces

nsantana
  • 1,992
  • 1
  • 16
  • 17