Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Java Encapsulation and OOP

MediumCoding00:00
Practice interviewer
In session
5 left
00:00

Your question is Java Encapsulation and OOP. Take a moment with it on the right.

Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).

You need to log in / sign up to chat or submit.

Problem

Accion Labs uses this class internally on a QA automation project to track failures for one test suite run. It's meant to be a textbook example of encapsulation for a junior engineer to study.

import java.util.ArrayList;
import java.util.List;

public class TestExecutionReport {
    public String suiteName;
    public List<String> failedCases = new ArrayList<>();
    public static List<TestExecutionReport> ALL_REPORTS = new ArrayList<>();

    public TestExecutionReport(String suiteName) {
        this.suiteName = suiteName;
        ALL_REPORTS.add(this);
    }

    public void recordFailure(String caseId) {
        failedCases.add(caseId);
    }

    public List<String> getFailedCases() {
        return failedCases;
    }

    public int getFailureCount() {
        return failedCases.size();
    }

    public void setSuiteName(String name) {
        suiteName = name;
    }
}

Explain whether this class actually achieves encapsulation, and walk through each specific OOP concept it's supposed to demonstrate, pointing out exactly what's wrong with each one. Answer in writing.