Java OOP, Core Concepts &
Collections Framework
A Complete Interview Preparation Guide
with Real-Time Selenium Automation Examples
Topics Covered
The Four Pillars of OOP: Encapsulation, Inheritance, Polymorphism, Abstraction
Interfaces & Abstract Classes
Constructors, static, and final
Core Java Fundamentals & Operators
Java Collection Framework
Rapid-Revision Interview Cheat Sheets
Java OOP & Collections Interview Guide Page 1
PART 1 | OBJECT-ORIENTED PROGRAMMING
1. What is OOP (Object-Oriented Programming)?
OOP is a programming paradigm where everything is represented as objects. It helps make code reusable,
maintainable, scalable, and secure.
The Four Pillars of OOP
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
2. Encapsulation
Definition
Encapsulation means binding data (variables) and methods together in one class while restricting direct
access to data. Private variables are accessed using getters and setters.
Example
class Employee {
private int salary;
public void setSalary(int salary) {
[Link] = salary;
}
public int getSalary() {
return salary;
}
}
Employee emp = new Employee();
[Link](50000);
[Link]([Link]());
OUTPUT
50000
Java OOP & Collections Interview Guide Page 2
Why use Encapsulation?
• Data Security
• Prevent unauthorized access
• Easy maintenance
• Controlled modification
Real-Time Selenium Example
Instead of writing [Link](...) everywhere, we create a page class:
// [Link]
private By username = [Link]("user");
public void enterUsername(String name) {
[Link](username).sendKeys(name);
}
The locator is hidden. The test class only calls [Link]("Admin"); — this is
Encapsulation.
Q: Why are variables private?
Because users should not directly modify variables. Only methods should validate and update
values.
3. Inheritance
Definition
Inheritance means one class acquires properties and methods of another class. Keyword: extends
Example
Java OOP & Collections Interview Guide Page 3
class Animal {
void eat() {
[Link]("Eating");
}
}
class Dog extends Animal {
void bark() {
[Link]("Barking");
}
}
Dog d = new Dog();
[Link]();
[Link]();
OUTPUT
Eating
Barking
Why use Inheritance?
• Code Reusability
• Avoid duplicate code
• Easy maintenance
Real-Time Selenium Example
Suppose BaseTest contains driver initialization, browser launch/close, screenshots, and waits:
public class BaseTest {
WebDriver driver;
public void launchBrowser() {}
public void closeBrowser() {}
}
public class LoginTest extends BaseTest { }
Now LoginTest gets the driver, browser launch, and browser close without rewriting. This is Inheritance.
Types of Inheritance in Java
Java supports: Single, Multilevel, Hierarchical.
Java does not support Multiple inheritance using classes — but it does support Multiple inheritance using
Interfaces.
Java OOP & Collections Interview Guide Page 4
Q: Why doesn't Java support Multiple Inheritance with classes?
Because of the Diamond Problem — if two parent classes have the same method, Java cannot
decide which one to execute.
4. Polymorphism
Meaning: One object behaves differently in different situations. There are two types: Compile-time
Polymorphism and Runtime Polymorphism.
Compile-Time Polymorphism (Method Overloading)
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Runtime Polymorphism (Method Overriding)
class Animal {
void sound() {
[Link]("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog Bark");
}
}
Animal a = new Dog();
[Link]();
OUTPUT
Dog Bark
At runtime, the JVM decides which method to execute.
Java OOP & Collections Interview Guide Page 5
Real-Time Selenium Example
WebDriver driver;
driver = new ChromeDriver();
// or
driver = new FirefoxDriver();
The driver reference stays the same while the object changes — this is Runtime Polymorphism.
Overloading vs Overriding
Overloading Overriding
Same method name Same method name
Different parameters Same parameters
Compile time Runtime
Same class Parent-child class
5. Abstraction
Definition
Showing only essential functionality while hiding implementation. Achieved using an Abstract Class or an
Interface.
Example using Interface
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
[Link]("Car Started");
}
}
// Usage: [Link](); -- no need to know engine implementation
Real-Time Selenium Example
[Link]("[Link]
Java OOP & Collections Interview Guide Page 6
We don't know the HTTP request, browser communication, or socket handling — Selenium hides everything.
This is Abstraction.
Abstraction vs Encapsulation
Abstraction Encapsulation
Hides implementation Hides data
Focuses on What Focuses on How
Interface / Abstract class Private variables
Achieves simplicity Achieves security
OOP — Interview Q&A
Q: Explain OOP with examples.
OOP stands for Object-Oriented Programming. It organizes code into classes and objects and is
based on four pillars:
Encapsulation hides data using private variables and getters/setters. Inheritance allows child
classes to reuse parent class functionality. Polymorphism allows the same method or object to
behave differently, such as WebDriver driver = new ChromeDriver(); Abstraction hides
implementation details using interfaces or abstract classes. In Selenium, methods like
[Link]() expose functionality without revealing internal implementation. OOP makes code
reusable, maintainable, scalable, and easy to test.
Q: Difference between Abstraction and Encapsulation?
Abstraction hides implementation details and shows only essential functionality. Encapsulation
hides data and controls access using private variables with getters and setters. Abstraction is
achieved using interfaces or abstract classes; Encapsulation is achieved using access modifiers.
Q: Give a real-time example of Inheritance.
In Selenium automation, we create a BaseTest class containing common methods like browser
initialization, teardown, waits, and screenshot capture. Test classes such as LoginTest and
HomePageTest extend BaseTest, allowing them to reuse this functionality without duplicating code.
This is inheritance.
Java OOP & Collections Interview Guide Page 7
Q: What is Runtime Polymorphism?
Runtime polymorphism is achieved through method overriding. The method to execute is
determined at runtime based on the actual object. In Selenium, WebDriver driver = new
ChromeDriver(); or new FirefoxDriver(); uses the same WebDriver reference with
different implementations — a classic example of runtime polymorphism.
Q: Why doesn't Java support multiple inheritance with classes?
Java does not support multiple inheritance with classes because it can lead to the Diamond
Problem. The Diamond Problem occurs when a child class inherits the same method from two
parent classes, and the compiler cannot determine which parent's method should be used.
Java OOP & Collections Interview Guide Page 8
6. What is an Interface?
Definition
An interface is a blueprint that defines what a class should do, but not how it does it. A class implements an
interface using the implements keyword.
interface Vehicle {
void start();
}
class Car implements Vehicle {
@Override
public void start() {
[Link]("Car Started");
}
}
Vehicle v = new Car();
[Link]();
OUTPUT
Car Started
Why do we use interfaces?
• Achieve abstraction
• Support multiple inheritance
• Define a common contract
• Enable loose coupling
Selenium Example
WebDriver driver = new ChromeDriver();
// WebDriver -> Interface
// ChromeDriver -> Implementation class
driver = new FirefoxDriver();
driver = new EdgeDriver();
// ... without changing the rest of the code
Java OOP & Collections Interview Guide Page 9
Q: Explain Interfaces in your own words.
An interface defines a contract that implementing classes must follow. It supports abstraction and
multiple inheritance. In Selenium, WebDriver is an interface implemented by classes like
ChromeDriver and FirefoxDriver.
7. Abstract Class vs Interface
Abstract Class Interface
Uses abstract keyword Uses interface keyword
Can have abstract and concrete methods Can have abstract, default, and static methods
Can have constructors Cannot have constructors
Can have instance variables Only constants (public static final)
A class can extend only one abstract class A class can implement multiple interfaces
Uses extends Uses implements
When to use an Abstract Class?
• Classes share common code
• You want to provide some implemented methods
• You want common state (instance variables)
When to use an Interface?
• Different classes need to follow the same contract
• You want multiple inheritance
• You want loose coupling
Selenium Example: WebDriver is an interface because ChromeDriver, FirefoxDriver, and EdgeDriver all
provide different implementations of the same methods.
Q: Why do we use @Override?
@Override is an annotation that tells the compiler that a method is intended to override a method
from its parent class or interface.
Java OOP & Collections Interview Guide Page 10
Q: Why use an Interface instead of an Abstract Class?
We use an interface when we want to define a contract that multiple unrelated classes can
implement. An abstract class is used when classes share common code or state.
Example: ChromeDriver, FirefoxDriver, and EdgeDriver should all have methods like get(),
findElement(), close(), and quit(). Instead of writing these methods in every class separately, Java
defines the WebDriver interface, and each browser provides its own implementation.
Why Interface: supports multiple inheritance, promotes loose coupling, easier to replace
implementations, better flexibility.
Why not Abstract Class: an abstract class is best when all child classes share common
implementation or data. For browser drivers, the implementation is completely different for Chrome,
Firefox, and Edge — so an interface is the better choice.
8. Constructors
Q: What is a constructor?
A constructor is a special method that has the same name as the class and no return type. It is
automatically invoked when an object is created and is used to initialize the object's state.
Q: Can constructors be overloaded?
Yes. A class can have multiple constructors with different parameter lists.
Q: Can constructors be overridden?
No. Constructors are not inherited, so they cannot be overridden.
Constructor vs Method
Constructor Method
Initializes an object Performs an operation
Same name as the class Can have any valid name
No return type Has a return type or void
Called automatically when an object is created Called explicitly
9. static and final
Java OOP & Collections Interview Guide Page 11
static means the member belongs to the class, not to individual objects. There is only one copy of a static
variable or method, shared by all objects.
The final keyword restricts modification. It can be applied to:
a) Final Variable
final int age = 25; // Cannot be changed
b) Final Method
class A {
final void show() {} // Cannot be overridden
}
c) Final Class
final class Employee {} // Cannot be inherited
Q: Why is the main() method static?
So the JVM can call it directly without creating an object.
Q: Can a static method be overridden?
No. Static methods are hidden, not overridden.
Q: Can a final class be inherited?
No. A final class cannot be extended. Example: String is final.
Q: Difference between this and super?
this refers to the current object. super refers to the parent class object.
this() calls another constructor in the same class. super() calls the parent class constructor.
Java OOP & Collections Interview Guide Page 12
PART 2 | CORE JAVA FUNDAMENTALS
1. What is a Variable?
Q: What is a variable?
A variable is a named memory location that stores data, and its value can change during program
execution.
int age = 26;
String name = "Shubham";
2. Local, Instance, and Static Variables
Local Variable Instance Variable Static Variable
Declared inside a method Declared inside class, outside methods
Declared with static
Method scope Object scope Class scope
Created when method is called Created with each object Created once when class loads
Must be initialized before use Gets default value Gets default value
class Employee {
static String company = "Infosys"; // Static
int id; // Instance
void display() {
int salary = 50000; // Local
}
}
3. Primitive vs Non-Primitive Data Types
Primitive Data Types (8)
• byte
• short
• int
Java OOP & Collections Interview Guide Page 13
• long
• float
• double
• char
• boolean
int age = 25;
boolean flag = true;
Non-Primitive Data Types
• String
• Array
• Class
• Interface
• Objects
String name = "Java";
Q: Difference between primitive and non-primitive types?
Primitive data types store actual values, while non-primitive data types store references to objects.
4. Operator & Keyword Comparisons
= vs ==
= ==
Assignment operator Comparison operator
Assigns value Compares values or references
int a = 10;
[Link](a == 10);
Pre-increment (++i) vs Post-increment (i++)
int i = 5;
[Link](++i);
Java OOP & Collections Interview Guide Page 14
OUTPUT
int i = 5;
[Link](i++);
[Link](i);
OUTPUT
5
6
&& vs &
&& &
Logical AND Bitwise AND (also works with booleans)
Short-circuit Evaluates both operands
if (a > 5 && b > 10) { ... }
If the first condition is false, Java does not evaluate the second condition.
|| vs |
|| |
Logical OR Bitwise OR (also works with booleans)
Short-circuit Evaluates both operands
if (a > 5 || b > 10) { ... }
If the first condition is true, Java skips evaluating the second condition.
break vs continue
break continue
Exits the loop Skips current iteration
Java OOP & Collections Interview Guide Page 15
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
[Link](i);
}
OUTPUT
1
2
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
[Link](i);
}
OUTPUT
1
2
4
5
throw vs throws
throw throws
Used to throw an exception Declares exceptions a method may throw
Used inside a method Used in method signature
throw new ArithmeticException();
public void readFile() throws IOException
this vs super
this super
Refers to current object Refers to parent class
Calls current class constructor Calls parent class constructor
[Link] = name;
[Link]();
Java OOP & Collections Interview Guide Page 16
final vs finally vs finalize()
final finally finalize()
Keyword Block Method (deprecated)
Prevents modification Executes after try/catch Called by GC before object cleanup (deprecated)
final int x = 10;
try {
}
catch (Exception e) {
}
finally {
[Link]("Always executes");
}
Q: Summarize final, finally, and finalize().
final is used to restrict changes, finally is used in exception handling, and finalize() was a cleanup
method invoked by the garbage collector but is deprecated.
extends vs implements
extends implements
Used for class inheritance Used to implement an interface
One class can extend only one class A class can implement multiple interfaces
class Dog extends Animal
class ChromeDriver implements WebDriver
Java OOP & Collections Interview Guide Page 17
5. More Core Java Interview Q&A
Q: Why is the main() method static?
The JVM starts program execution by calling the main() method. If main() were not static, the JVM
would first need to create an object of the class to call it. Making main() static allows the JVM to
invoke it directly.
Q: Why are variables declared private in encapsulation?
Private variables prevent direct access from outside the class. Access is controlled through getter
and setter methods.
Benefits: data hiding, better security, controlled access, easier validation.
Q: What is the use of the new keyword?
The new keyword creates a new object in heap memory and calls the appropriate constructor.
Example: Student s = new Student();
What happens: memory is allocated in the heap, the constructor is called, and the reference
variable points to the new object.
Core Java Rapid Revision
Question One-Line Answer
Variable Named memory location to store data
Local vs Instance vs Static Method vs Object vs Class level
Primitive vs Non-Primitive Store values vs Store object references
= vs == Assignment vs Comparison
++i vs i++ Increment before use vs Use before increment
&& vs & Short-circuit AND vs Always evaluates both operands
break vs continue Exit loop vs Skip current iteration
throw vs throws Throw exception vs Declare exception
this vs super Current object vs Parent class
final vs finally vs finalize() Restrict changes vs Cleanup block vs Deprecated cleanup method
extends vs implements Inherit class vs Implement interface
Java OOP & Collections Interview Guide Page 18
Why main() is static? JVM can call it without creating an object
Why private? Data hiding and encapsulation
new keyword Creates an object in heap memory and calls the constructor
Java OOP & Collections Interview Guide Page 19
PART 3 | JAVA COLLECTION FRAMEWORK
1. What is the Collection Framework?
The Collection Framework is a set of classes and interfaces in Java used to store, manage, and manipulate
groups of objects dynamically.
It provides interfaces like:
• List
• Set
• Queue
• Map (part of the framework, but not a Collection)
Benefits
• Dynamic size
• Sorting
• Searching
• Easy insertion/deletion
• Reusable code
Q: Define the Collection Framework in one line.
The Collection Framework is a set of interfaces and classes that provides a standard way to store
and manipulate groups of objects dynamically.
2. Collection vs Collections
Collection Collections
Interface Utility class
Stores objects Provides utility methods
Parent of List, Set, Queue Has methods like sort(), reverse(), shuffle()
[Link](list);
[Link](list);
Java OOP & Collections Interview Guide Page 20
Q: Difference between Collection and Collections?
Collection is an interface used to store objects, whereas Collections is a utility class that provides
static methods to perform operations on collections.
3. List vs Set
List Set
Allows duplicates Doesn't allow duplicates
Maintains insertion order HashSet doesn't, LinkedHashSet does
Index-based No indexing
List<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Java");
OUTPUT
[Java, Java]
Set<String> set = new HashSet<>();
[Link]("Java");
[Link]("Java");
OUTPUT
[Java]
4. ArrayList vs LinkedList
ArrayList LinkedList
Dynamic array Doubly linked list
Fast searching (get()) Slower searching
Slow insertion/deletion in middle Fast insertion/deletion
Less memory More memory (stores links)
Java OOP & Collections Interview Guide Page 21
Q: Which is better, ArrayList or LinkedList?
ArrayList is better for frequent searching, whereas LinkedList is better for frequent insertion and
deletion.
5. HashSet vs LinkedHashSet vs TreeSet
HashSet LinkedHashSet TreeSet
No order Maintains insertion order Automatically sorted
Fastest Slightly slower Slowest
No duplicates No duplicates No duplicates
HashSet<Integer> set = new HashSet<>();
[Link](30);
[Link](10);
[Link](20);
// Output order is not guaranteed
TreeSet<Integer> set = new TreeSet<>();
// ... add same elements
OUTPUT
[10, 20, 30]
6. HashMap vs Hashtable
HashMap Hashtable
Not synchronized Synchronized
Faster Slower
Allows one null key No null key
Allows null values No null values
Q: Difference between HashMap and Hashtable?
HashMap is faster but not thread-safe. Hashtable is synchronized, making it thread-safe but slower.
Java OOP & Collections Interview Guide Page 22
7. HashMap vs TreeMap
HashMap TreeMap
No ordering Keys are sorted
Faster Slower
Allows one null key Null keys are not allowed
8. Why doesn't Map extend Collection?
Because Map stores key-value pairs, while Collection stores only individual objects.
Collection Map
Apple 101 -> Shubham
Banana 102 -> Rahul
Orange
Q: Why doesn't Map extend Collection?
Map doesn't extend Collection because it stores key-value pairs instead of individual elements.
Since their structures are different, Map has its own hierarchy.
Java OOP & Collections Interview Guide Page 23
9. Iterator vs ListIterator
Iterator ListIterator
Works with all collections Works only with List
Forward only Forward and backward
Remove supported Add, remove, set supported
Iterator<String> it = [Link]();
ListIterator<String> it2 = [Link]();
10. Quick-Fire Collections Facts
Q: Which collection would you use to remove duplicates?
Use HashSet. Example: HashSet set = new HashSet<>();
Q: Which collection maintains insertion order?
ArrayList, LinkedList, LinkedHashSet, and LinkedHashMap.
Q: Which collection automatically sorts data?
TreeSet and TreeMap.
TreeSet<Integer> set = new TreeSet<>();
OUTPUT
10
20
30
Q: Which collection allows duplicate elements?
List, ArrayList, LinkedList, and Vector.
Java OOP & Collections Interview Guide Page 24
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Java");
OUTPUT
[Java, Java]
11. Why is ArrayList faster than LinkedList for searching?
Because ArrayList stores elements in contiguous memory (dynamic array), so it can access an element
directly using its index.
[Link](500); // Fast in ArrayList
In LinkedList, Java must traverse nodes one by one to reach the 500th element, making it slower.
Q: Why is ArrayList faster for searching?
ArrayList is faster for searching because it provides direct index-based access, whereas LinkedList
traverses nodes sequentially.
12. How Do You Iterate Through a Collection?
Using for-each loop
ArrayList<String> list = new ArrayList<>();
for (String name : list) {
[Link](name);
}
Using Iterator
Iterator<String> it = [Link]();
while ([Link]()) {
[Link]([Link]());
}
Using for loop (List only)
Java OOP & Collections Interview Guide Page 25
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
}
Collections Rapid Revision
Question Answer
Collection Framework Framework to store and manipulate groups of objects
Collection vs Collections Interface vs Utility class
List vs Set Duplicates allowed vs Unique elements
ArrayList vs LinkedList Fast search vs Fast insert/delete
HashSet vs LinkedHashSet vs TreeSet No order vs Insertion order vs Sorted
HashMap vs Hashtable Not synchronized vs Synchronized
HashMap vs TreeMap Unordered vs Sorted by keys
Why Map not Collection? Stores key-value pairs
Iterator vs ListIterator Forward vs Forward + Backward
Remove duplicates HashSet
Insertion order ArrayList, LinkedList, LinkedHashSet, LinkedHashMap
Automatic sorting TreeSet, TreeMap
Duplicate elements List
Why ArrayList is faster? Direct index access
Iterate collection for-each, Iterator, for loop
Java OOP & Collections Interview Guide Page 26