2

I'm trying to implement interface like this :

public interface Human{

    void talk();
}



public class Ame implements Human{

    public static void talk(){
        System.out.println("Speak English");
    }
}



public class Chin implements Human{

    public static void talk(){
        System.out.println("Speak Chinese");
    }
}


public class test {

    public static void main(String[] args){

        Chin c = new Chin();
        c.talk();
        Ame a = new Ame();
        a.talk();
} 
}

But it shows errors :
Ame and Chin talk() cannot implement Human talk().
Methods is overridden as static .
Please tell me why this heppened and how to fix this error.

3 Answers3

3

Static methods are part of Class and not Objects. Overriding is concept of polymorphism, ie, a method associated with an instance can have multiple behaviour.

Static methods are not associated with instance and polymorphism cannot be applied.

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
0

When you declare a method as static, it belongs to the class as a whole and not a specific instance. The methods of an interface cannot be static in Java. When you implement an interface, you are expected to provide an instance method for the abstract methods of the interface. When you use a static method, your static method tries to hide the instance method of the same name. But this would violate the rules to be followed while implementing an interface. Thus we cannot make the interface methods as static in the implementing class.

Aditya Gupta
  • 633
  • 1
  • 4
  • 11
0

You cannot reference a non-static interface from a static method this way. In essence, a static method is one that can be accessed directly without recreating a local duplicate object, but its values cannot be modified in the same way. Really, the solution to this problem is quite simple. Remove the static modifier from the overriding talk() methods