how to implement a method from an interface java code example
Example 1: Interface in java
interface Vehicle
{
public void accelerate();
}
class BMW implements Vehicle
{
public void accelerate()
{
System.out.println("BMW accelerating...");
}
}
public class InterfaceDemo
{
public static void main(String[] args)
{
BMW obj = new BMW();
obj.accelerate();
}
}
Example 2: java implement interface
interface Appendable {
void append(string content);
}
class SimplePrinter implements Appendable {
public void append(string content) {
System.out.println(content);
}
}
class FileWriter implements Appendable {
public void append(string content) {
}
}
Example 3: how to implement a interface in java
interface methods{
public static hey();
}
class scratch implements methods{
public static hey(){
System.out.println("Hey");
}
}
Example 4: Interface in java
interface InterfaceName
{
fields
methods
}