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.
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
1/* File name : Animal.java */
2interface Animal {
3 public void eat();
4 public void travel();
5}
1// interface syntax
2interface InterfaceName
3{
4 fields // by default interface fields are public, static final
5 methods // by default interface methods are abstract, public
6}