- 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.
- 軟件安全技術(shù)
- TypeScript Blueprints
- Microsoft Dynamics 365 Extensions Cookbook
- Learning RabbitMQ
- 跟老齊學(xué)Python:輕松入門
- Python數(shù)據(jù)可視化之Matplotlib與Pyecharts實戰(zhàn)
- Learning DHTMLX Suite UI
- 大數(shù)據(jù)分析與應(yīng)用實戰(zhàn):統(tǒng)計機器學(xué)習(xí)之?dāng)?shù)據(jù)導(dǎo)向編程
- Odoo 10 Implementation Cookbook
- Clojure for Java Developers
- Java程序設(shè)計教程
- Java EE互聯(lián)網(wǎng)輕量級框架整合開發(fā):SSM+Redis+Spring微服務(wù)(上下冊)
- Learn C Programming
- MATLAB/Simulink與過程控制系統(tǒng)仿真
- web2py Application Development Cookbook