---Advertisement---

JAVA OOPS Interview Questions and Answers (Level-1)

By Manisha

Published On:

---Advertisement---

1. What is Encapsulation, and how is it used in frameworks like Selenium or TestNG?

Answer

Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It means bundling the data (variables) and the methods (functions) that operate on the data into a single unit, like a class. It also involves restricting direct access to some of the object’s components, typically by using private access modifiers, and providing public getter/setter methods to interact with them.

Real-World Example in Selenium:

In Selenium test automation, we often use Page Object Model (POM). Each web page is represented as a class, and the elements on the page are declared as private WebElement.

java

public class LoginPage {

    private WebElement usernameField;

    private WebElement passwordField;

    public void enterUsername(String username) {

        usernameField.sendKeys(username);

    }

}

Here, usernameField is encapsulated and only accessed via a public method.

In TestNG:

TestNG uses annotations like @Test, @BeforeClass, etc., to execute tests. You don’t need to expose the internal logic of test setup or teardown.

java

@Test

public void loginTest() {

    // test steps here

}

Encapsulation hides complex setup logic from the test method consumer, improving code modularity and security.


2. What is Abstraction and how does it help in test automation design?

Answer

Abstraction means hiding the complex implementation details and exposing only the essential features of an object or functionality. This helps simplify the way you interact with code components.

In Test Automation:

Suppose you have multiple test cases that need to click the login button. Instead of writing the driver.findElement() code repeatedly, you abstract it in a reusable method:

java

public void clickLoginButton() {

    driver.findElement(By.id(“login”)).click();

}

Now every test case can just call clickLoginButton() without worrying about how the button is located. This improves readability, maintainability, and reduces code duplication.


3. Difference between Abstract Class and Interface. When should you use each?

Answer

FeatureAbstract ClassInterface
Can have method implementation?✅ Yes❌ (Java 7) ✅ (Java 8+ with default methods)
Can have constructors?✅ Yes❌ No
Inheritance supportSingle inheritanceMultiple inheritance
Use caseWhen you have shared base functionalityWhen you want to define rules (contracts)

When to Use in Test Automation:

  • Use Abstract Class for shared base logic like test setup:

java

public abstract class BaseTest {

    @BeforeMethod

    public void setup() {

        // common setup

    }

}

  • ✅ Use Interface for defining utility contracts or listeners:

java

public interface BrowserActions {

    void openBrowser(String browserName);

}

Interfaces help decouple the implementation from the definition and are ideal for defining reusable behaviors.


4. Explain Compile-time and Runtime Polymorphism with real-world examples.

Answer

Compile-time Polymorphism (Method Overloading):

Occurs when multiple methods in the same class have the same name but different parameter lists.

java

public class LoginPage {

    public void login(String username) {

        System.out.println(“Login with username”);

    }

    public void login(String username, String password) {

        System.out.println(“Login with username and password”);

    }

}

Selenium Example: Overloading clickElement():

java

public void clickElement(By locator) { … }

public void clickElement(WebElement element) { … }

Runtime Polymorphism (Method Overriding):

Occurs when a subclass overrides a method from the parent class.

java

class WebPage {

    public void openPage() {

        System.out.println(“Opening generic page”);

    }

}

class LoginPage extends WebPage {

    @Override

    public void openPage() {

        System.out.println(“Opening login page”);

    }

}

WebPage page = new LoginPage();

page.openPage(); // Output: Opening login page

Selenium Example: You can override setup() in every test class for browser-specific configuration.


5. Is it Method Overloading if a Method has Different Access (Private, Public) Modifiers? Explain.

Answer

No, method overloading is not determined by access modifiers.

Method Signature = Method Name + Parameter List

It does not include:

  • Return type
  • Access modifiers (private, public, protected)

Incorrect Overloading:

java

public void display() {}

private void display() {} // Not overloading – Compilation Error

Correct Overloading:

java

public void display(String name) {}

public void display(int id) {} // ✅ Overloaded method

Overloading should be based on number/type/order of parameters, not on access levels.


6. What are Access Modifiers in Java and their role in building a test automation framework?

Answer

Access Modifiers define the visibility or scope of a class, method, or variable.

ModifierSame ClassSame PackageSubclass in Other PackageOutside Package
private✅ Yes❌ No❌ No❌ No
default (no modifier)✅ Yes✅ Yes❌ No❌ No
protected✅ Yes✅ Yes✅ Yes❌ No
public✅ Yes✅ Yes✅ Yes✅ Yes

Use in Test Automation:

  • private for WebElements inside Page Object Classes:

java

private WebElement loginButton;

  • public for exposing actions:

java

public void clickLoginButton() {}

This helps protect internal implementation, while allowing controlled access to actions.


7. What is the difference between HashMap and Hashtable? Which is thread-safe?

Answer

FeatureHashMapHashtable
Thread-safe❌ No✅ Yes
PerformanceFaster (no sync)Slower (synchronized)
Allows null keys/values?✅ Yes (1 null key, many null values)❌ No
UsageSingle-threaded applicationsMulti-threaded applications

In Test Automation:

  • Use HashMap to store test data when only one thread is accessing it:

java

Map<String, String> testData = new HashMap<>();

testData.put(“username”, “admin”);

  • Use Hashtable if you’re running parallel tests and need thread safety, though using ConcurrentHashMap is more common in modern projects.

👉The Next 7 Questions -2: JAVA OOPS

---Advertisement---

Leave a Comment