- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 215字
- 2021-07-02 19:01:55
Static method in an interface
The static method in an interface is similar to the static method in a class. Users cannot override them. So even if a class implements an interface, it cannot override a static method of an interface.
Like a static method of a class, static method in an interface is called by using an interface name or class type reference, not using instance variables:
public interface MyInterface { static String hello() { return "Inside static method in interface"; } void absmethod(); }
Here is an interface with an abstract method and a static method:
public class MyInterfaceImpl implements MyInterface{ @Override public void absmethod() { System.out.println("Abstract method implementaion in class"); } }
Here is the class that implements the interface given previously, so to adhere to the contract it has to provide implementation of the abstract method as it is not an abstract class. However, it does not need to implement the static method.
Now, let's try to call the static method:
public class MyInterfaceDemo { public static void main(String[] args) { System.out.println(MyInterface.hello()); // it works MyInterfaceImpl obj =new MyInterfaceImpl(); obj.hello(); // wont-complie } }
So, calling the static method using the interface name reference works, but it does not work using the instance variable to a class that implements the interface.
- AngularJS入門與進階
- Ext JS Data-driven Application Design
- Production Ready OpenStack:Recipes for Successful Environments
- Java游戲服務器架構實戰
- C語言程序設計
- Python自然語言處理(微課版)
- 精通Python自然語言處理
- SAP BusinessObjects Dashboards 4.1 Cookbook
- 快速念咒:MySQL入門指南與進階實戰
- Mastering Drupal 8 Views
- Learning Salesforce Einstein
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- 從Power BI到Analysis Services:企業級數據分析實戰
- INSTANT Apache ServiceMix How-to
- 人人都能開發RPA機器人:UiPath從入門到實戰