Q14. List and explain about OOP concepts.
Answer:
The various functional concepts of OOP are as follows
(a) Objects
(b) Classes
(c) Data abstraction
(d) Encapsulation
(e) Inheritance
(f) Polymorphism
(a) Objects
An object is an instance of a class and it represents the real world entities in a class. It is basic run time entity (unit) in an object-oriented system. A problem in programming is analyzed in terms of objects and nature of communication between them.
When a program is executed, objects interact with each other by sending messages. Different objects (of other classes can collaborate work together) with each other without knowing the details of their data (or) code.
Example
For a class named student its data members will be like Student_Name, Roll No, Student_Marks etc. The different objects can have different values for these data members as each object can be used to represent the information of real world person. For example,
Feroz, 47, 74
Nayeem, 39, 82
(b) Classes
A class is an abstract data type that groups the data and its associated functions. It'can also hide the data and functions if required.
A class specification consists of two parts.
(i) Declaration of a class and
(ii) Definitions of member functions of a class.
The members scope and type are described by the class declaration and implementation of member functions are described by the class function definitions.
General Form
class name
{
private:
Variables and functions declarations
public:
variables and function declarations
};
The class keyword indicates an abstract data type called name of the class. The body of the class includes the variables and function declarations:
(c) Data Abstraction
Abstraction is one of the important element of OOPS for managing complexities. It is a mechanism that hides the implementation details and shows only the functionality. For example, the end user just type the password while performing transaction on ATM and accepts the services provided to him.
But the user is unaware of the internal processing. Thus, an abstraction is one that mainly focuses on what the object does but not how it does.
(d) Encapsulation
Encapsulation is a mechanism of binding data members and corresponding methods into a single module called class inorder to protect them from being accessed by the outside code. An instance of a class can be called as an object and it is used to access the members of a class. In encapsulation, objects are treated as ‘black boxes’ since each object performs specific task.
The data and functions available in a class are called as members of a class. The data defined in the class are called as member variables or data members and the functions defined are called as member functions.
The main idea behind the concept of encapsulation is to obtain high maintenance and to handle the application’s code.
(e) Inheritance
Inheritance can be defined as a mechanism of acquiring features from one class called parent class to another class called child class. Parent class is also known as base class or super class and child class is known as derived class or sub class. The parent class contains only its own features whereas the child class contains the features of both parent as well as the child classes. Moreover, an object created for parent class can access only by the parent class members. However, child class object can access members of both child class as well as parent class.
The child class can acquire the properties of parent class using the keyword ‘extends’.
(f) Polymorphism
Polymorphism is one of the OOPs concept. It can be used to design and implement systems that can be extensible more easily. ‘poly’ means many and ‘morph’ mean forms i.e., polymorphism is the ability to take more than one form. There are two types of polymorphism. They are static polymorphism and dynamic polymorphism.