Selenium/Java-Interface

In last post, we learnt about Polymorphism in Java which was the last main OOPs concept. In this post, we will go through the concept of Interface and will see how it is extremely useful while writing codes.

An Interface in Java is just like a class but it implicitly contains only Abstract and Public methods that has only signature but not method body where as class can contain public, private and protected methods.  Implements keyword is used in Interface just like extends keyword is used in class. 

An Interface can be defined as a blue print of the class as when the class implements the Interface it needs to override all the methods defined in Interface. When the class does not implements all the methods defined in an Interface, we need to define that particular class as Abstract. 

[Source: www.ashtpoint.com ]


An Interface is used mainly for two reasons:
  • We can achieve complete or 100% abstraction as all the methods defined in an Interface are abstract.  
  • Multiple inheritance is not supported by class in Java but it can be achieved using an Interface as one class can implements more than one Interfaces.
An Interface can not be instantiated and also we can not create objects of an Interface but it can contain constants and it will always be public, static and final.

Example:

// declaring Interface

Interface Bank{

// abstract methods in interface

void interestRate();

}

// class implements interface

class xBank implements Bank{

void interestRate(){

System.out.println(" Rate of Interest is 5%");

}

}

// class implements interface

class yBank implements Bank{

void interestRate(){

System.out.println(" Rate of Interest is 7%");

}

// main method

public static void main(String[] args){

Bank b=new xBank();
b.interestRate();

Bank c=new yBank();
c.interestRate();

}

}

Run this code and also try to implement multiple interfaces in one class, observe the results !!

Cheers ! 



















Comments

Post a Comment

More posts on selenium...

Selenium/Java-OOPs-Encapsulation

Selenium/Java-OOPs-Abstraction

Selenium: Automate Web Application Login

Selenium/Java- CoC

Selenium/Java-OOPs-Inheritance

Selenium: Waits

Selenium: All about XPath Locator in WebDriver

Selenium: Capturing the screenshot

Selenium: BDD- The Disruptive Framework

Selenium: Frameworks