As a part of End to End REST Assured Tutorial , in this post We will learn about Interface in Java.
I will strongly recommended you to go through the topic Abstraction – Hide the implementation before continuing this post.
Let’s start with a real time example. You would be reading this post on a mobile or a laptop. Let’s assume you are reading this post on your laptop. You will be scrolling post up or down through arrow keys or mouse pad. After reading this post, You may be interested to search more topics in search box of my blog. You will type search queries in search box using keypad. Are you worrying about how key is typed or what is internal working architecture of laptop is? You are just concerned about work to be done. Can I say that you and laptop are INTERFACED by screen , keypad and mouse pad?
Laptop has exposed their required parts so that anybody can use it. Those exposed part is called Interface. The same concept is also applicable in OOPS (Object Oriented Programming System ) like Java. Javadoc explains interface as “Objects define their interaction with the outside world through the methods that they expose. Methods creates the object’s interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. ” Interface and abstract class are ways to achieve Abstraction in Java.
Points about Interface:-
- We can create an interface using “interface” keyword.
- We can have abstract , default and static methods in an interface. From Java 8 , an interface can have concrete methods as well.
- We can not instantiate an interface.
- We can not create a constructor in an interface.
- Data members in an Interface is public, static and final by default.
- Abstract methods are public by default. A method without body will be abstract and public by default.
- Multiple inheritance can be achieved using interface.
- An interface can extend one or multiple interfaces.
- An interface neither extend nor implement a class.
- An interface can not have static or non-static blocks.
- An interface without its implemented class is incomplete.
You will be thinking why I am talking about basics of Interface in REST Assured tutorials. We will connect dots now. This concept is even important to understand Selenium WebDriver hierarchy of classes and interfaces as well.
When we perform a HTTP verb request, we get response as a type of Response which is an interface. Then we perform many operation on them. You may confuse an interface has no method definitions ( Except default and static concrete methods ) then how we are able to use it. How interface will decide which implementation to be called? I will try to explain this connection with very simple example.
Let’s create an interface with two methods in it :-
I1.java:
package JavaConcepts; public interface I1 { // Two abstract methods void printSomething(); void printAnything(); }
We know an interface without its implemented class or classes is incomplete , so let’s create two classes who implement above interface I1 :-
ImplementInterface1.java
package JavaConcepts; public class ImplementInterface1 implements I1{ // Implemented abstract methods of I1 @Override public void printSomething() { System.out.println("Print something from first implementation"); } @Override public void printAnything() { System.out.println("Print anything from first implementation"); } }
ImplementInterface2.java
package JavaConcepts; public class ImplementInterface2 implements I1{ // Implemented abstract methods of I1 @Override public void printSomething() { System.out.println("Print something from second implementation"); } @Override public void printAnything() { System.out.println("Print anything from second implementation"); } }
Let’s create a class who use above created implemented class:-
Usage.java:-
package JavaConcepts; public class Usages { // Return type is I1 but returning an object of ImplementInterface1 which implements I1 public static I1 someMethod1() { return new ImplementInterface1(); } // Return type is I1 but returning an object of ImplementInterface2 which implements I1 public static I1 someMethod2() { return new ImplementInterface2(); } }
Note above that return type of both methods have same i.e. I1 but actual content is different for both. First method is returning an object of ImplementInterface1.java and second ImplementInterface2.java. “I1” is just a reference to hold any as it is super type of both classes.
Now run some tests: –
SomeTests.java:-
package JavaConcepts; import org.testng.annotations.Test; public class SomeTest { @Test public void m1() { // Same reference type but different implementation is called I1 ref1 = Usages.someMethod1(); ref1.printSomething(); ref1.printAnything(); I1 ref2 = Usages.someMethod2(); ref2.printSomething(); ref2.printAnything(); } }
Output:-
Print something from first implementation Print anything from first implementation Print something from second implementation Print anything from second implementation
Observe the above output. Different implementations were called but have similar type of interface reference. This is also called runtime polymorphism or method overriding.
In the same way , interfaces and its implemented classes work in Selenium WebDriver and REST Assured . Return type may be an Interface type implementation class keeps the key.
You can clone/download example repo here.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.
I1 ref1 = Usages.someMethod1();
can we create object of interface if my understaing is not correct can you please explain this line again
Wow!!! Really Good Explanation. Thanks Amod. Great work.
Thanks Pramod.
I just love the way you teach. It’s a basic concept which many might not be knowing. it’s awesome.
thanks, please keep creating this
i had to read 3 times to understand this. Very good and detail knowledge you have here, the last line is the key to remember
Different implementations were called but have similar type of interface reference
I1 ref1 = Class1.method1();
I1 ref2 = Class1.method2()
Return type may be an Interface type, “implementation class keeps the key.”
An interface with its implemented class is incomplete.
Correction needed as
An interface without its implemented class is incomplete.
Thanks. Done.