top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Interface And Its Role In Selenium




Overview


In this article as a beginner SDET I have explained what is an Interface and how it is related to Selenium and what is the difference between Interface and Abstract class. To understand how interfaces work, you will need to know what a class is and how inheritance works, so read up on those first if necessary.



Table Of Contents


Data Abstraction

In Java, data abstraction is a way of hiding specific details of a class, showing the user only what they need to know. In other words user will have the information on what the object does instead of how it does it. One way to achieve data abstraction in Java is using abstract classes and methods. To make a class or method abstract, you simply define it with the abstract keyword.


Abstract Classes and Methods

  • Classes which are declared with the abstract keyword is known as abstract class in Java.

  • It can have both abstract (method without body) and non-abstract methods (method with body).

  • Abstract classes achieve partial abstraction.


Sample Code

abstract class CentralBank{
  public abstract void minBalance();//method without body
  public void neft() {//method with body
    System.out.println("All banks maximum Neft transaction amount should be 50,000 rupees");
  }
}

The Central Bank class above is an abstract class, so it cannot be used to create an object. Trying to do so will generate an error. To access an abstract class, it must be inherited from another class called the subclass. This subclass must then implement the body of every abstract method declared inside the abstract class. See the example below:

public class CanaraBank extends CentralBank {//Canara bank inherits from Central bank and implements its abstract methods.

 public static void main(String[] args) {
  CanaraBank cb=new CanaraBank(); //Accessing Central bank class through    Canara bank class.
  cb.neft();
  cb.minBalance();

 }

Java interfaces take this a step further by applying abstraction to the entire class, not just specific methods. Now we will get a clear picture what Interface is all about.


What is Interface?


  • Interface is a kind of protocol that sets up rules regarding how a class should behave. Also, we can say it is like a blue print or contract to the class.

  • Interface is a group of related methods with empty bodies. It is the class responsibility to implement the methods declared in the body.

  • In a nutshell Interface will have method names but no implementation will be done (no body part). But from Java 8 onwards this picture of Interface gets changed which has been discussed in this blog.

  • By default, all the methods in the interface are public and abstract.


Where Interface concept is applied in our real life


With the help of credit card one can purchase or do any transactions in dollars, or pounds or rupees or any other currency. E-commerce giants like Amazon, eBay all accept credit card payment with different currency. Credit card is like an interface that performs several tasks. Credit card does not hold money in a physical form. Then how the shopkeepers or these online e-commerce companies can get the money. Behind the credit card, have our individual bank a/c details from where the actual money is transferred after authentication. The bank a/c is the implementation class and credit card are an interface.


Sample code


public  interface CreditCard {
 
 public void purchaseInDollars();
 public void purchaseInPounds();
 public void purchaseInRupees();

public class AmericanBank implements CreditCard{

 public static void main(String[] args) {
  
AmericanBank ab=new AmericanBank();
ab.purchaseInDollars();
ab.purchaseInPounds();
ab.purchaseInRupees();
ab.limitInDollar();
ab.limitInPounds();
ab.limitInRupees();
 }

 @Override
 public void purchaseInDollars() {
  
  System.out.println("Dollars accepted ");
 }

 @Override
 public void purchaseInPounds() {
  
  System.out.println("Pounds accepted");
  
 }

 @Override
 public void purchaseInRupees() {
 
  System.out.println("Rupees accepted");
 }
 public void limitInDollar() {
  System.out.println("No max limit for dollars");
 }
 public void limitInPounds(){
  System.out.println("No max limit for pounds");
 }
 public void limitInRupees() {
  System.out.println("No max limit for rupees ");
 }

}

Types of Interfaces


In general, the interfaces are categorized into

1.Normal Interface

2.Functional Interface

3.Marker Interface


Interface Concept in Selenium


  • For example, to run in Chrome browser Selenium people have developed a class called Chrome Driver. This Chrome Driver class will have all the methods which will help you to automate in chrome browser.

  • get (), getCurentUrl(), getTitle(), findElements(), findElement(), quit() and close() are few methods present inside the Chrome Driver, Firefox Driver, Edge Driver and Internet Explorer Driver classes. There are many more methods inside these driver classes.

  • To use these methods we create an object of that class. For ex ChromeDriver driver=new ChomeDriver();

  • The beauty of Selenium is all these classes will have the same method name,but only inner implementation will change.

  • How do these classes have common method names? Here comes the concept of Interface.

  • Have you ever understood why we write the code as WebDriver driver=new ChromeDriver(); instead of ChromeDriver driver=new ChromeDriver();

  • WebDriver is an interface. Classes like Firefox,Chrome,Microsoft edge etc. will follow the rules imposed by WebDriver interface and each class will implement the methods. You can also refer https://www.selenium.dev which is the selenium official website where you will find the WebDriver interface and its implementing classes.

  • Hence we write the syntax as WebDriver driver=new ChromeDriver(); driver object has access to the methods of Chrome Driver which are defined in Web Driver interface.

Normal Interface


Normal interface is the one were only abstract methods are allowed stritcly. No implementation inside the method body.


Functional Interface

  • Functional Interface is introduced from Java 8 onwards.

  • Till Java 7 only Abstract methods were allowed.

  • If any Interface has only one Abstract method it is called as functional interface. To remember usually called as SAM (single abstract method).

  • Apart from one Abstract method any number of static and default methods are allowed in the Interface.

  • There is a strong relation between this Functional Interface and Lambda expressions. Again, Lambda expression is introduced from Java 1.8 onwards and that is another separate interesting topic.

Marker Interface


It is an empty interface which means no methods. Examples of this interface are Cloneable, Remote and Serializable. This is again a separate topic to be deep dived into.


Difference between Interface and Abstract class


In most interviews this is one of the famous question asked.

Abstract Class

Interface

  1. abstract keyword

  1. interface keyword

2. Subclasses extends abstract abstract class

2.Subclasses implements interfaces

3. Abstract classes can have implemented methods and zero or more abstract methods.

3. Java 8 onwards interfaces can have static and default methods.

4. We can extend only one abstract class

4. We can implement multiple interfaces.

5. Methods can have access modifiers public, private, protected and static

5. Methods are implicitly public and abstract only.


Summary

In this blog we saw what is data abstraction and interfaces and were this concept is related in selenium.



1,148 views1 comment

Recent Posts

See All

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Guest
Apr 25
Rated 5 out of 5 stars.

Good one!

Like
bottom of page