Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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.

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

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

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");
                
        }

        
}

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

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

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

Uncategorized

Post navigation

Previous Post: How to Query JSON using JSONPath?
Next Post: TestNG Tutorials 41: Skipping a Test Conditionally in TestNG

Related Posts

Page Factory In Selenium Webdriver: Inbuilt Page Object Model Of Selenium Uncategorized
August 27, 2017 – Make Selenium Easy Uncategorized
January 25, 2019 – Make Selenium Easy Uncategorized
July 16, 2019 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Postman Tutorial Part 44 – Data Driven Testing in Postman Using CSV File Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark