Interview Q&A / Object-Oriented Programming
Object-Oriented Programming interview questions
The four pillars, interfaces, abstract classes and design basics.
Prefer rapid revision? Practise Object-Oriented Programming short interview answers →
- What is encapsulation, and how do access modifiers help achieve it?
Encapsulation means keeping an object's data and the methods that change it together, and hiding the data so outside code can only change it through controlled methods. In Java, access modifiers (private, default, protected, public) decide who can see each member, and making fields private with validated methods is the usual way to encapsulate.
BeginnerIn-depth3 min read - Is encapsulation the same as data hiding?
Not exactly. Encapsulation bundles data and the methods that work on it into one unit. Data hiding is the part where the fields are made private so outsiders can only use the safe methods.
BeginnerQuick answer1 min read - What is the difference between abstraction and encapsulation?
Abstraction decides what to show: it exposes a simple view and hides complexity. Encapsulation decides how to protect: it bundles data with methods and restricts access to it.
BeginnerQuick answer1 min read - Why use getters and setters when we could make fields public?
Methods let a class validate input, compute values, log changes, and change its internal storage later without breaking callers. A public field gives up all of that control.
BeginnerQuick answer1 min read