1// interface in java example
2interface Vehicle
3{
4 public void accelerate();
5}
6class BMW implements Vehicle
7{
8 public void accelerate()
9 {
10 System.out.println("BMW accelerating...");
11 }
12}
13public class InterfaceDemo
14{
15 public static void main(String[] args)
16 {
17 BMW obj = new BMW();
18 obj.accelerate();
19 }
20}
1// Assume we have the simple interface:
2interface Appendable {
3 void append(string content);
4}
5// We can implement it like that:
6class SimplePrinter implements Appendable {
7 public void append(string content) {
8 System.out.println(content);
9 }
10}
11// ... and maybe like that:
12class FileWriter implements Appendable {
13 public void append(string content) {
14 // Appends content into a file
15 }
16}
17// Both classes are Appendable.
1interface methods{
2 public static hey();
3}
4
5class scratch implements methods{
6 // Required to implement all methods declared in an interface
7 // Or else the class becomes abstract
8 public static hey(){
9 System.out.println("Hey");
10 }
11}
1/* File name : MammalInt.java */
2public class MammalInt implements Animal {
3
4 public void eat() {
5 System.out.println("Mammal eats");
6 }
7
8 public void travel() {
9 System.out.println("Mammal travels");
10 }
11
12 public int noOfLegs() {
13 return 0;
14 }
15
16 public static void main(String args[]) {
17 MammalInt m = new MammalInt();
18 m.eat();
19 m.travel();
20 }
21}
1Interfaces specify what a class must do.
2It is the blueprint of the class.
3It is used to achieve total abstraction.
4We are using implements keyword for interface.
5
6Basic statement we all know in Selenium is
7WebDriver driver = new FirefoxDriver();
8WebDriver itself is an Interface.
9So we are initializing Firefox browser
10using Selenium WebDriver.
11It means we are creating a reference variable
12of the interface and creating an Object.
13So WebDriver is an Interface and
14FirefoxDriver is a class.
15
16
1public interface Exampleinterface {
2
3 public void menthod1();
4
5 public int method2();
6
7}
8
9class ExampleInterfaceImpl implements ExampleInterface {
10
11
12 public void method1()
13 {
14 //code here
15 }
16
17 public int method2()
18 {
19 //code here
20 }
21
22}