To call the methods of an interface, you need to create a class that implements the interface, and then call the methods of the interface just like calling methods of other classes. Interfaces cannot be instantiated, only implemented. Methods in interfaces are abstract by default and cannot have constructors. An interface variable can refer to an instance of any class that implements the interface. Two interfaces cannot be directly related through inheritance, but it is possible to create classes that implement multiple interfaces.
How to call interface in Java
In Java, an interface is a class that contains only abstract methods. Interfaces cannot be instantiated, but they can be implemented. A class that implements an interface must provide concrete implementations of all abstract methods in the interface.
How to call the interface
To call the method of the interface, you need to first create a class that implements the interface. The interface's methods can then be called just like any other class's methods.
For example:
<code class="java">// 定义一个接口 interface Shape { void draw(); } // 实现接口 class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing a rectangle"); } } // 调用接口的方法 Shape shape = new Rectangle(); shape.draw();</code>
Output:
<code>Drawing a rectangle</code>
Note:
The above is the detailed content of How to call interface in java. For more information, please follow other related articles on the PHP Chinese website!