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}
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}
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
1interface Animal {
2 void child();
3}
4class Cat implements Animal {
5 public void child() {
6 System.out.println("kitten");
7 }
8}
9class Dog implements Animal {
10 public void child() {
11 System.out.println("puppy");
12 }
13}
14public class LooseCoupling{
15 public static void main(String args[]) {
16 Animal obj = new Cat();
17 obj.child();
18 }
19}
1package com.concretepage;
2
3@FunctionalInterface
4public interface Calculator {
5 long calculate(long num1, long num2);
6}
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}