Make Selenium Easy

REST Assured Tutorial 6 – Interface in OOPS – Implement As You Wish

Last updated on December 4th, 2019 at 03:15 pm

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:-

  1. We can create an interface using “interface” keyword.
  2. We can have abstract , default and static methods in an interface. From Java 8 , an interface can have concrete methods as well.
  3. We can not instantiate an interface.
  4. We can not create a constructor in an interface.
  5. Data members in an Interface is public, static and final by default.
  6. Abstract methods are public by default. A method without body will be abstract and public by default.
  7. Multiple inheritance can be achieved using interface.
  8. An interface can extend one or multiple interfaces.
  9. An interface neither extend nor implement a class.
  10. An interface can not have static or non-static blocks.
  11. 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:

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

ImplementInterface2.java

Let’s create a class who use above created implemented class:-

Usage.java:-

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:-

Output:-

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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

3 thoughts on “REST Assured Tutorial 6 – Interface in OOPS – Implement As You Wish

  1. 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.”

  2. An interface with its implemented class is incomplete.
    Correction needed as
    An interface without its implemented class is incomplete.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 6 – Interface in OOPS – Implement As You Wish

by Amod Mahajan time to read: 4 min
3
%d bloggers like this: