Can we make a static method in interface ? if yes then how we call that method from an interface ?

is it possible to make a method Static in Interface ? if yes then how to call that method form interface ?

  • Sahil
  • 16 May
  • 7622 Views
  • 7 Answers
Your Answer

yes... from version 1.8 it is available but up to 1.7 interfaces support only abstract methods..
if you declared static method in interface it distrub to all interface impleamented class...follow this code


package youthforwork;

public interface Youth {
    
     void run();
    static void test()
    {
        System.out.println(" we can declare static method ");
    }
    
    default void test1()
    {
        System.out.println("but it is available from 1.8 only");
    }
    

}


package youthforwork;

public class Implemented implements Youth {
    
    public void run()
    {
        System.out.println(" executing / calling interface (STATIC) method in implemented class");
    }
    
    public static void main(String[] args) {
        
        
        
        
        Youth y=new Implemented();
        y.run();
        y.test1();
        Youth.test();
    }

}



EXECUTE THIS CODE 

Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here