WebDriver And Interface
Q. What Is An Interface And Why To Use It?
Interface is set of rules for behavior of application. Interface looks like class but it is not. When you implements that Interface In any class then all those Interface rules must be applied on that class.
In short, if you implement an Interface on
class then you must have to override all the methods of Interface In your
class. Interface will create Rules to follow structure for class where it Is
Implemented. This way, if you look at the code of Interface, You can get Idea
about your program business logic. When you are designing big architecture
applications like selenium web driver, you can use Interface to define business
logic at Initial level.
Interface can be implemented with any class
using implements keyword. There are set of rules to be followed for creating an
Interface. Let me tell you all these rules first and then give you an example
of an Interface.
1. Interface
can not hold constructor.
2. Interface
can not hold instance fields/variables.
3. Interface
can not hold static methods.
4. You can not
instantiate/create object of an Interface.
5. Variables
Inside Interface must be static and mandatory to initialize the variable.
6. Any class
can Implement Interface but can not extend Interface.
7. Can write
body less methods Inside Interface.
8. By default
all the methods and variables of Interface are public so no need to provide
access modifiers.
public interface College { //Interface file
static
String Collegename = "XYZ";
//static by default
void
StudentDetails(); //Created non static
methods without body.
void
StudentResult();
}
public class Computer implements College {
//@Override
annotation describes that methods are overridden on interface method.
//Methods name, return type are same as
methods of an Interface.
@Override
public
void StudentDetails() {
System.out.println("hi"); }
@Override
public
void StudentResult() { System.out.println("bye"); }
}
public class TestCollege
{ //Class file. No need to implement
Interface.
public static void
main(String[] args)
{
//Can access Interface variable directly using
Interface name.
System.out.println(College.Collegename+"
Collage student details.");
//Created
Computer class object with reference of interface College.
College
compdept = new Computer();
//Methods
will be called from Computer class.
compdept.StudentDetails();
compdept.StudentResult();
}
}
-------------------------------------------------------------------------------------------------------------------------
Selenium
WebDriver And Interface
Simple example of Interface In selenium WebDriver Is
WebDriver Interface. When you are Initializing any browser using selenium
WebDriver, You are writing statements like bellow.
WebDriver driver = new FirefoxDriver();
Or
WebDriver driver = new ChromeDriver();
You can view more examples of selenium WebDriver on
THIS PAGE.Here, WebDriver Is Interface and FirefoxDriver and ChromeDriver are
the class files where WebDriver Interface Is Implemented.
http://software-testing-tutorials-automation.blogspot.in/2014/04/interface-in-java-tutorials-for.html