Nepal Police2080 BSQuestion 10 of 14
Explain abstraction and encapsulation with example. How does polymorphism work in object oriented programming? Write.
10 marks
Question source
- Exam year
- 2080 BS / 2024 AD
- Level
- Sub-Inspector (Prabidhik – IT/Computer)
- Paper
- Paper II – Professional and Service Specific Test
- Section
- Section B
Model answer
OOP models real-world entities as objects with data + behaviour.
Abstraction — show what, hide how:
Expose only essential features. Example: car.start() button — driver need not know fuel-injection code. In Java: abstract class Shape { abstract float area(); } — user calls area() without knowing circle vs rectangle formula.
Encapsulation — bundle + protect: Wrap data and methods in one class and hide internals with private + public getters/setters.
class Citizen {
private String citizenshipNo; // hidden
public String getNo() { return citizenshipNo; }
public void setNo(String n) { if(n.length()==10) citizenshipNo=n; }
}
Direct obj.citizenshipNo="x" is blocked; validation inside setter prevents wrong IDs — data integrity.
Polymorphism — same name, different forms:
- Compile-time (overloading): Same method name, different parameters.
add(a,b)vsadd(a,b,c)— compiler picks by signature. - Runtime (overriding): Child redefines parent method.
Animal.sound()→Dog.sound()barks,Cat.sound()meows. At runtimeAnimal a = new Dog(); a.sound();plays bark.
Benefit: one interface (payTax()), many implementations (salary vs business) — extensible code for e-governance modules.