Tulasi Nepali Learning Platform
tulasinepali.com.np • Official Educational & Exam Preparation Notes
Overview & Definition (परिचय तथा परिभाषा)
Object‑Oriented Programming (OOP) is a programming paradigm that models software as a collection of objects—entities that combine data (attributes) and behavior (methods). By organizing code around real‑world concepts, OOP promotes reuse, scalability, and maintainability, which are essential for large‑scale applications and for candidates preparing for Loksewa and computer operator examinations.
Core Concepts with Detailed Sub‑sections (मुख्य अवधारणाहरू)
1. Class and Object (क्लास र वस्तु)
A class is a blueprint defining attributes (variables) and methods (functions). An object is an instance of a class, possessing its own state while sharing the class’s behavior.
2. Encapsulation (इनक्याप्सुलेशन)
Encapsulation hides internal data behind public interfaces, protecting integrity and reducing coupling. In C++ it is achieved using private/protected access specifiers; in Java, the same keywords apply.
3. Inheritance (इनहेरिटेन्स)
Inheritance enables a new class (subclass/derived class) to acquire properties of an existing class (superclass/base class). It supports hierarchical classification and code reuse.
4. Polymorphism (पोलिमोर्फिज्म)
Polymorphism allows a single interface to represent multiple underlying forms. It manifests as:
- Compile‑time (static) polymorphism: method overloading and operator overloading (C++ only).
- Run‑time (dynamic) polymorphism: method overriding via virtual functions in C++ and overridden methods in Java.
5. Abstraction (एब्स्ट्र्याक्शन)
Abstraction focuses on essential features while ignoring irrelevant details. Abstract classes and interfaces provide contracts that concrete classes must fulfill.
Key Points / Rules / Functions
- All objects are instances of a class; a class can exist without any objects.
- Encapsulation is enforced through access modifiers:
private,protected,public. - Single inheritance is mandatory in Java; C++ supports multiple inheritance.
- Virtual functions in C++ enable dynamic dispatch; Java methods are virtual by default (except
finalorstatic). - Constructors initialize object state; destructors (C++) clean up resources, while Java relies on garbage collection.
- Interfaces in Java can contain only abstract methods (pre‑Java 8) or default/static methods (post‑Java 8). C++ uses pure virtual classes to achieve similar behavior.
Comparison / Summary Table
| Aspect | C++ | Java |
|---|---|---|
| Paradigm | Multi‑paradigm (procedural, OOP, generic) | Purely OOP (except primitive types) |
| Memory Management | Manual (new/delete) + RAII | Automatic garbage collection |
| Inheritance | Single & multiple inheritance | Single inheritance; multiple via interfaces |
| Polymorphism | Virtual functions (run‑time), function/operator overloading (compile‑time) | All non‑static methods are virtual; method overloading only |
| Access Specifiers | public, protected, private, friend | public, protected, private (no friend) |
| Exception Handling | Supported but not mandatory | Mandatory checked exceptions for certain APIs |
| Standard Library | STL (containers, algorithms) | Java Collections Framework |
| Platform Dependency | Compiled to native code; platform‑specific binaries | Bytecode runs on JVM – write once, run anywhere |
Practical Application / Real‑world Relevance
Understanding OOP building blocks equips candidates to:
- Design modular software for government information systems, where maintainability is critical.
- Choose the right language for a project: C++ for performance‑intensive tasks (e.g., GIS, embedded systems) and Java for cross‑platform enterprise applications.
- Interpret and write code snippets in exam papers, especially in sections that test concepts like inheritance hierarchies, abstract classes, and exception handling.
Quick Review Questions & Answers / Practice MCQs (अभ्यासका लागि प्रश्नोत्तर)
- Q: Which OOP concept is demonstrated by declaring data members as
privateand providingpublicgetter/setter methods?
A: Encapsulation. - Q: In C++, which keyword makes a function eligible for run‑time polymorphism?
A:virtual. - Q: Which language allows multiple inheritance directly?
A: C++. - Q: In Java, what must a class do to become an interface?
A: Use theinterfacekeyword; it cannot contain instance fields (except static final). - Q: Which of the following is NOT a feature of Java?
A) Operator overloading
B) Automatic garbage collection
C) Platform independence
D) Strongly typed language
A: A) Operator overloading. - Q: Identify the correct statement about constructors in C++ and Java.
A: Both languages call a constructor automatically when an object is created; C++ may also have a destructor, while Java relies on garbage collection.
Remember: Mastery of OOP concepts transcends any single language. Focus on the ideas—encapsulation, inheritance, polymorphism, abstraction—and you can apply them equally well in C++, Java, or any modern language.