Interface In Java : Tutorials For Selenium Webdriver

I described about Inheritance and method overriding in java software development language In my previous post so I am suggesting you to read that post, Specially method overriding concept before learning about Interface In Java software development. If you will go for selenium webdriver Interview then Interviewer will ask you first question : What Is WebDriver? Peoples will give different different answers but right answer Is WebDriver Is an Interface. So It Is most Important thing for all selenium webdriver software testing tool learner to understand an Interface properly.

What Is An Interface And Why To Use It?

Using Interface, We can create a contract or we can say set of rules for behavior of software application. Interface Is looks like class but It Is not class. When you implements that Interface In any class then all those Interface rules must be applied on that class. In sort, 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 software applications like selenium webdriver, 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.
  • Interface can not hold constructor.
  • Interface can not hold instance fields/variables.
  • Interface can not hold static methods.
  • You can not instantiate/create object of an Interface.
  • Variables Inside Interface must be static and mandatory to initialize the variable.
  • Any class can Implement Interface but can not extend Interface.
  • Can write body less methods Inside Interface.
  • By default all the methods and variables of Interface are public so no need to provide access modifiers.

How To Add Interface In Your Project

To add Interface In your project, Right click on package and go to -> New -> Interface. It will open New java Interface popup. Give any name to Interface(Example : College) and click on save button. It will add College.java Interface file under your package.



Now let us look at simple example of Interface.

  • Create One Interface file with name = College.java as shown In bellow example.
  • Create 3 class file with name = Computer.java, Mechanical.java and TestCollege.java as shown In bellow example.
College.java
public interface College {//Interface file
 //Initialized static variable. 
 //No need to write static because It Is by default static.
 String Collegename = "XYZ";
 
 //Created non static methods without body.
 void StudentDetails();
 void StudentResult();
}

Computer.java
//Class file Implemented with Interface file using implements keyword.
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("Computer Dept. Student Detail Part");
 }

 @Override
 public void StudentResult() {
  System.out.println("Computer Dept. Student Result Part");  
 }
}

Mechanical.java
//Class file Implemented with Interface file using implements keyword.
public class Mechanical implements College{

 @Override
 public void StudentDetails() {
  System.out.println("Mechanical Dept. Student Detail Part");   
 }

 @Override
 public void StudentResult() {
  System.out.println("Mechanical Dept. Student Result Part");  
 } 
}

TestCollege.java
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();
  
  //Created Mechanical class object with reference of interface College.
  College mecdept = new Mechanical();
  //Methods will be called from Mechanical class.
  mecdept.StudentDetails();
  mecdept.StudentResult();    
 }
}

Now If you will run TestCollege.java file, Output will looks like bellow.

XYZ Collage student details.
Computer Dept. Student Detail Part
Computer Dept. Student Result Part
Mechanical Dept. Student Detail Part
Mechanical Dept. Student Result Part

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.

Have you read article on mostly asked selenium with java interview questions about abstract class on THIS PAGE and difference between interface and abstract class on THIS PAGE?

17 comments:

  1. Thank you. Very informative

    ReplyDelete
  2. Thank you. Explained very clearly.

    ReplyDelete
  3. Thank you, can't expect more that this.. :-)

    ReplyDelete
  4. Great work.Thank you very much :):):)

    ReplyDelete
  5. Very easy to understand. gr8 work sir...

    ReplyDelete
  6. Thanks for sharing the informative topics.

    ReplyDelete
  7. very well explained.. Thanks for creating this article. Few spelling mistake are there :)

    ReplyDelete
  8. Thanks brother, this is extremly well explained.

    I was very confused what is interface and what it does? this article explained it very well.
    Thanks Once Again.

    ReplyDelete
  9. hi, I am getting the Error while executing TestCollege.java:
    It says that "Cant' convert from Computer class to College Interface"
    It says that "Can't convert from Mechanical class to College Interface"

    Please help me in this...

    ReplyDelete
  10. - override is not allowed implementing interfaces. you can only implement the contract that is set the inteface College.

    public class Computer implements College {
    @Override //this is not allowed
    public void StudentDetails() {
    System.out.println("Computer Dept. Student Detail Part");
    }

    ReplyDelete