interface implementation in java code example

Example 1: Interface in java

// interface in java example
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

// Assume we have the simple interface:
interface Appendable {
	void append(string content);
}
// We can implement it like that:
class SimplePrinter implements Appendable {
 	public void append(string content) {
   		System.out.println(content); 
    }
}
// ... and maybe like that:
class FileWriter implements Appendable {
 	public void append(string content) {
   		// Appends content into a file 
    }
}
// Both classes are Appendable.

Example 3: how to implement a interface in java

interface methods{
 public static hey(); 
}

class scratch implements methods{
  // Required to implement all methods declared in an interface 
  // Or else the class becomes abstract
  public static hey(){
   System.out.println("Hey"); 
  }
}

Example 4: java interface

public interface Exampleinterface {
	
  public void menthod1();
  
  public int method2();

}

class ExampleInterfaceImpl implements ExampleInterface {


  	public void method1()
    {
        //code here
    }
  
    public int method2()
  	{
    	//code here
  	}

}

Example 5: how to declare a interface in java

// Assume we have the simple interface:
interface Appendable {
	void append(string content);
}
// We can implement it like that:
class SimplePrinter implements Appendable {
 	public void append(string content) {
   		System.out.println(content); 
    }
}
// ... and maybe like that:
class FileWriter implements Appendable {
 	public void append(string content) {
   		// Appends content into a file 
    }
}
// Both classes are Appendable.
0
how to implement a interface in java

Tags:

Misc Example