Object Oriented Paradigm - Simplifies Software Maintenance
Object Oriented Paradigm
- The Object-Oriented Programming (OOP) paradigm is a programming methodology based on the concept of objects.
- It is a widely used programming paradigms.
- It is characterized by the organization of code into reusable and self-contained units called objects. An object is an instance of a class. A class models a Real-World Entity.
- Objects represent real-world entities and encapsulate both data (attributes) and the methods (functions or procedures) that operate on that data.
OOPs corresponds to real world. It is standard or a perspective used to imitate or model the real world. This paradigm is used to develop a software to overcome the drawbacks as seen and discussed in structured programming. It assumes the world to be a collection of Entities.
Entity:
Its dictionary meaning is "a thing with distinct and independent existence". An entity is an person, place, thing or a concept about which one needs to store certain information and it performs certain actions.
Example: A student is an entity. Information like name, email, mobile number, address etc. A student also performs actions - attends lectures, appear in exam and attends practical.
The two significant features of OOPs that is two of the four pillars of Object-Oriented Programming to understand -
Data Abstraction:
It is the process of finding out the information and actions required of an entity in the problem domain. It is also used to determine the relationship among entities. It is widely used in Object Oriented Analysis and Design (OOAD) to identify entity and relationship among entities.
Data Hiding or Data Encapsulation:
It is the process of hiding the implementation details from the user. It ensures security of data. For example - A person uses a bike without knowing how it works.
Write a program to accept and display a number using Object Oriented Paradigm.
Here the question in developer or programmer's mind is –
- What entities are involved in the problem domain?
- What information and actions are required of identified entities?
- What relationship exists among the entities?
To get answer, read the question again and again.
The identified Entity is -
- Number (as it is being accepted and displayed)
The information required of the identified entity
- Value (Magnitude of the Number)
- accept
- display
Object oriented languages (C++. Java, C#.NET, VB.NET etc. ) provide a feature called a class to model an entity in the program or the software being developed.
Class:
- A user-defined datatype
- It is used to model an entity.
- Information required of the entity becomes data-members in C++ terminology, fields or an attributes in terms of Java, C#.NET, VB.NET, Python etc. or state in Object analysis and design terminology.
- Actions required of an Entity becomes Member functions is C++ terminology or Methods in Java, C#.NET, VB.NET, Python etc. or behavior in OOAD terminology.
- It is like a blueprint to implement entity in a program.
class classname {
Access Modifier:
//data member or field declarations
Access Modifier:
//Member functions or Methods Definitions
};
Now the identified entity from the problem is implemented in C++ by defining a class as follows:
class Number {
private:
//data member or field declarations
int n;
public:
//Member functions or Methods Definitions
void accept() {
std::cin>>n;
}
void display() {
std::cout<<"\n n = "<<n;
}
};
The above defined class is just a model or blueprint of the identified entity in the given problem. It represents the entity in the program. Recall, a class is a user-defined datatype and also recall built-in datatypes (Ex. int or float). To use a datatype in program, its variable is created or declared in the program. Similarly, a variable of a Class is termed as an instance or an object of a class in OOPs terminology. Memory is allocated for a declared variable, therefore when object is created, memory is allocated for its data-members or fields.
A variable is created or declared as - datatype variable-name;
In C Language, int n;
In C++ Language (Object-Oriented Language), the Object or an instance of a class is created as - class-name object-name;
Example: - Number number;
The member functions or the methods of the class are called with dot (.) operator as -
object-name. Method-name();
Finally the main() function to test the above defined class.
int main() {
// Write testing code here
//Create Object of Number
Number number;
//Calling member functions
number.accept();
number.display();
//Error - Not accessible
//number.n =10;
return 0;
}
Wandering:
- What's the use of Access Modifiers?
- Why Information required of an entity is under private section?
- Why Member functions or methods in the public section?
The use of access modifiers is used to achieve Data Hiding or enforce Data Encapsulation.
How?
The data-members or fields declared in the private section of the class is not accessible outside the class. Only the member functions or methods of the class can access it or process it.
The member functions or methods are defined in the public section so as to make it accessible outside the class.
Object Oriented Paradigm - Simplifies Software Maintenance
Reviewed by Syed Hafiz Choudhary
on
September 17, 2023
Rating:
No comments: