Object-Oriented Programming
Class, Object, Encapsulation, Inheritance & Polymorphism
Complete NEB Class 12 Computer Science notes on Object-Oriented Programming тАФ class, object, encapsulation, abstraction, inheritance, polymorphism, and OOP vs POP тАФ with fully solved MCQs and short answer questions.
Core Concepts of OOP
The relatively newer concept of developing a computer program with an approach to designing a reusable program is called OOP.
The features of OOP are as follows:
Object
Everything around ourselves is an object in OOP. An object is the basic runtime entity in OOP which consists of data and functions. It stores data in variables.
Class
Generally, a class is a category/group which is used to identify some people or things in this world. A class provides the structure of an object and defines the prototype of the object.
Data Abstraction
It is data hiding. We can show only necessary information and can hide the unnecessary part. Abstraction is one of the key concepts of OOP languages.
Encapsulation
The wrapping of related data and functions (that operate on data) into a single unit (called a class) is known as encapsulation.
Polymorphism
The term polymorphism is formed by the combination of two Greek words: ‘poly’ (means many) and ‘morph’ (means form), i.e., the ability to take more than one form.
The relatively newer concept of developing a computer program with an approach to designing and reusable programs is known as OOP.
Advantages
Disadvantages
The applications of OOP are as follows:
The process by which objects of one class acquire the similar properties of objects of another class from which they are derived is called inheritance.
Types of inheritance:
1. Single inheritance
In single inheritance, a class derives from one base class only. This means that there is only one subclass that is derived from one superclass.
2. Multiple inheritance
Multiple inheritance is a type of inheritance in which a class derives from more than one class. (Class C is a subclass that has Class A and Class B as its parents).
3. Multilevel inheritance
A derived class that is created from another derived class is called multi-level inheritance. This inheritance can have many levels as long as our implementation doesn’t go wayward.
4. Hybrid Inheritance
Hybrid inheritance is usually a combination of more than one type of inheritance (single, hierarchical, and multi-level).
5. Hierarchical inheritance
In hierarchical inheritance, more than one class inherits from a single base class. This gives it a structure of a hierarchy.
| S.N. | Structured Programming / POP | Object-Oriented Programming (OOP) |
|---|---|---|
| 1 | Structured programming is designed focusing on process/logical structure and then the data required for that process. | Object-oriented programming is designed focusing on data. |
| 2 | Structured programming follows a top-down approach. | Object-oriented programming follows a bottom-up approach. |
| 3 | Structured programming is less secure as there is no way of data hiding. | Object-oriented programming is more secure as it has a data hiding feature. |
| 4 | Structured programming cannot solve complex programs easily. | Object-oriented programming can solve any complex programs. |
| 5 | Structured programming provides less reusability and more function dependency. | Object-oriented programming provides more reusability and less function dependency. |
| 6 | Less abstraction and less flexibility. | More abstraction and more flexibility. |
Old is Gold: Multiple Choice Questions
Select an option to view the correct answer and justification.
Justification: Inheritance is the OOP feature that allows a class (child) to acquire the properties and behaviors of another class (parent).
Justification: Inheritance enables a new class to reuse the attributes and methods of an existing class, avoiding duplicate code.
Justification: Encapsulation is the wrapping of related data and functions that operate on that data into a single unit called a class.
Justification: Exception handling is an error-handling mechanism, not one of the core OOP concepts (which include class, encapsulation, and polymorphism).
Justification: Encapsulation refers to wrapping data and its related functions together into a single unit, and often hiding the internal details.
Justification: Inheritance lets a new class reuse the code of an existing class instead of rewriting it, making it the feature most directly tied to reusability.
Justification: OOP is the standard abbreviation for Object-Oriented Programming, a paradigm based on objects and classes.
Justification: An object represents a well-defined “thing” with clear boundaries and relevance to the problem being modeled, holding data and functions.
Justification: A class is a blueprint or category that groups similar objects sharing common properties, behavior, and relationships.
Justification: Inheritance is precisely the property that lets a new class be built on top of an existing class, reusing its members.
Short Answer Questions
Object
Everything around ourselves is an object in OOP. An object is the basic runtime entity in OOP which consists of data and functions. It may represent a person, place, bank account, table of data, or any item that the program has to handle. It stores data in variables. Every object has two things: properties and behavior.
Example: Object – Student
Class
Generally, a class is a category/group which is used to identify some people or things in this world. A class provides the structure of an object and defines the prototype of the object. Every class has three important things: name, attributes/features, and operations/functions.
Example:
class Item {
private:
int code;
float price;
public:
void get_data();
void display_data();
};
Inheritance is a key concept in OOP that allows one class (called the child or subclass) to acquire the properties and behaviors (attributes and methods) of another class (called the parent or superclass). This promotes code reusability and a hierarchical class structure.
Example: Consider a parent class “Vehicle” and child classes like “Car” and “Bike”.
The characteristics of OOP are as follows:
Polymorphism
Polymorphism is a concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It means “many forms” and it enables one interface to be used for different data types. There are two types of polymorphism:
Inheritance
Inheritance is a mechanism in OOP where one class (subclass or child class) acquires the attributes and methods of another class (superclass or parent class). This allows for code reuse and establishes a relationship between different classes. The subclass can add its own unique methods and properties in addition to the inherited ones. There are two main types of inheritance:
Procedural-oriented programming (POP) is a programming paradigm that follows a step-by-step approach to solve problems, where the program is divided into small, manageable procedures or functions. Each function performs a specific task and those functions are executed sequentially to achieve the desired outcome. POP focuses on procedures or routines that operate on data and control the flow of execution. In POP, the primary focus is on the sequence of actions that need to be performed to achieve a result.
Object-oriented programming (OOP) is a paradigm that organizes software design around objects, which are instances of classes that encapsulate both data and behavior (methods). It emphasizes encapsulation, inheritance, polymorphism, and abstraction to create modular, reusable, and maintainable codes.
In contrast, procedural-oriented programming (POP) focuses on functions or procedures that operate on global data, with a linear flow of execution. The main difference is that OOP structures code around objects and their interactions, whereas POP organizes codes around functions and a sequence of steps leading to operate. OOP provides more flexibility, scalability, and reusability compared to POP.
Tools used in program design:
In OOP, inheritance is a mechanism to share code and behavior. It allows a programmer to derive the behavior of a class in the definition of new classes. It is the process by which objects of one class acquire similar properties of objects of another class from which they are derived, providing the ability to use an existing class to create a new one. It is a relationship between two or more classes.
Types of Inheritance:
1. Single Inheritance
In single inheritance, a class derives from one base class only. This means that there is only one subclass that is derived from one superclass.
Fig: Single Inheritance
2. Multiple Inheritance
Multiple inheritance is a type of inheritance in which a class derives from more than one class.
Fig: Multiple Inheritance
3. Multilevel Inheritance
A derived class that is created from another derived class is called multilevel inheritance. This inheritance can have as many levels as long as our implementation doesn’t go wayward.
Fig: Multilevel Inheritance
4. Hybrid Inheritance
Hybrid inheritance is usually a combination of more than one type of inheritance (single, hierarchical, and multi-level).
Fig: Hybrid Inheritance
5. Hierarchical Inheritance
In hierarchical inheritance, more than one class inherits from a single base class as shown in the representation below. This gives it a structure of a hierarchy.
Fig: Hierarchical Inheritance
ЁЯУД View / Download Full PDF Notes
If the PDF doesn’t load, click here to open it in Google Drive.
