- 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.
- Advanced Splunk
- Data Visualization with D3 4.x Cookbook(Second Edition)
- 高手是如何做產(chǎn)品設(shè)計的(全2冊)
- Web前端開發(fā)技術(shù):HTML、CSS、JavaScript(第3版)
- 零起步玩轉(zhuǎn)掌控板與Mind+
- Visual Basic學(xué)習(xí)手冊
- Oracle從入門到精通(第5版)
- Node.js Design Patterns
- Scratch3.0趣味編程動手玩:比賽訓(xùn)練營
- Building Wireless Sensor Networks Using Arduino
- Hands-On GUI Programming with C++ and Qt5
- Learning AWS
- Hands-On JavaScript for Python Developers
- HTML5 WebSocket權(quán)威指南
- React.js實戰(zhàn)