2 - POP - OOP - OOPs Concepts
2 - POP - OOP - OOPs Concepts
⮚ A program in a procedural language is a list of instructions where each statement tells the computer
to do something.
⮚ It focuses on procedure (function).
⮚ When a program becomes larger, it is divided into functions & each function has a clearly defined
purpose.
⮚ Most of the functions share global data.
⮚ Global data is accessible by all functions.
⮚ Global data is more unsafe to an accidental change by a function because if a function makes any
changes to global data, these changes will reflect in other functions.
⮚ Each function may have its own local data.
⮚ Procedure oriented Programming follows Top Down Approach in problem solving.
⮚ It emphasizes doing things. Data is given a second class status even though data is the reason for
the existence of the program.
⮚ Since every function has complete access to the global variables, the new programmer can corrupt
the data accidentally by creating a function. Similarly, if new data is to be added, all the functions
need to be modified to access the data.
⮚ It is often difficult to design because the components function and data structure do not model the
real world.
Object Oriented Programming
⮚ The major motivating factor in the invention of object-oriented approach is to remove some of the
drawbacks encountered in the procedural approach.
⮚ OOP treats data as a critical element in the program development and does not allow it to flow
freely around the systems.
⮚ It ties data more closely to the functions that operate on it, and protects it from accidental
modification from outside functions.
⮚ OOP allows decomposition of a problem into a number of entities called objects and then builds
data and functions around these objects.
⮚ The data of an object can be accessed only by the function associated with that object.
⮚ Data is hidden and cannot be accessed by external functions.
⮚ However, functions of one object can access functions of other objects.
⮚ OOP Follows bottom-up approach in program design.
⮚ Object Oriented Programming has the following basic and necessary concepts such as:
❖ Object
❖ Class
❖ Data Abstraction
❖ Encapsulation
❖ Data Hiding
❖ Inheritance
❖ Polymorphism
❖ Dynamic Binding
1. Object
⮚ Any real world entity that has state and behavior is known
as an object. For example, a chair, pen, table, keyboard,
bike, etc.
⮚ It can be physical or logical.
⮚ In OOP, an Object can be defined as an instance of a class.
⮚ Objects are also referred to as the basic runtime entity
because during execution of the program objects are
essential.
⮚ An object contains an address and takes up some space in
memory.
⮚ Objects can communicate without knowing the details of each other's data or code.
⮚ The only necessary thing is the type of message accepted and the type of response returned
by the objects.
⮚ Different objects have different states or characteristics and behaviour.
⮚ In OOP, the states or characteristics of an object is referred as member variable or data
members which is used to hold data
⮚ The behaviour of an object can be implemented through member functions or methods.
Example:
Real world object (Book)
State or characteristics Behaviour
Chapters Used to read
Pages Used to prepare notes
Topics
OOP programming object(Student)
Name Acceptdata()
Class Display()
Marks
2. Class
⮚ Class is a group of similar types of objects, which possess some attributes and behaviour.
⮚ It describes the nature of an object.
⮚ Example: If we consider Fruits as a class then apple, orange, mango, banana are considered
as objects of the class Fruits.
⮚ We can create a class within another class also for some application (if required).
⮚ Class are considered as user defined datatypes whereas objects can be considered as
variables of the class.
⮚ Example: If Automobile is considered as a class then TwoWheeler, ThreeWheeler and
FourWheeler are considered as the subc-classes of the class Automobile.
⮚ In OOP, both class and objects are dependent on each other or in other words they are
considered as two sides of a coin.
Attributes of a Class:
⮚ Every object has some characteristics and behaviour.
⮚ The characteristics of an object are called data members and the behavior are called
member methods or member functions or simply methods in a class.
⮚ These data members and member functions are called as attributes of the class.
⮚ Example:
Class Student: Attributes
Name Getdata( )
Roll_Number Dispdata( )
Class
Section
Contact_Number
Mail_id
4. Encapsulation
⮚ In OOP data are not allowed to move freely from functions to functions
hence are kept securely inside a class.
⮚ Data are allowed to be accessed by the member functions of that class.
⮚ Hence, it is necessary to bind the data members and member functions
into a single unit.
⮚ Binding (or wrapping) of the data members and member functions into a single unit is
known as Data Encapsulation.
⮚ As a medicine capsule is wrapped with different medicines.
5. Data Hiding
⮚ The insulation of data from the outside world by using the concept of Encapsulation is
called Data hiding.
6. Inheritance
⮚ The process of creating a new class from the existing class is called Inheritance.
⮚ The existing class is called the base class.
⮚ The newly created class from the base class is called a derived class.
⮚ The derived class can acquire all the properties of the base class.
⮚ Code reusability is one of the benefits of inheritance.
⮚ Code Reusability means the codes or the implementation procedures of the base class can
be used in the derived class (No need to rewrite the code).
⮚ There are five different types of inheritances such as
● Single Inheritance
● Multilevel Inheritance
● Multiple Inheritance (Java does not support directly)
● Hierarchical Inheritance
● Hybrid Inheritance
7. Polymorphism
⮚ Poly means many and morphos means forms. Hence, polymorphism means many forms.
⮚ It describes the feature of languages that allows the same word or symbol to be interpreted
correctly in different situations based on the context.
Example:
In java when the symbol ‘+’ used with numbers performs addition operation and when used
with strings performs concatenation operation.
10 + 20 = 30
“comp” + “uter” = “computer”
⮚ There are two types of Polymorphism available in Java.
● Static polymorphism: It is otherwise called compile time polymorphism.
Example: Method Overloading
● Dynamic polymorphism: It is otherwise called as Runtime polymorphism
Example: Method Overriding
8. Dynamic Binding
⮚ It is the link that is established between the Method call and Method signature
⮚ Dynamic binding also called dynamic dispatch
⮚ It is the process of linking procedure calls to a specific sequence of code (method) at run-
time.
⮚ The compiler generates a coded address of the function signature and applies it at the
function call position in the caller program.
⮚ The control gets transferred to the address available at the function call position, at the time
of execution. This process is called dynamic binding
⮚ The compiler generates the machine code address of the function calculate() say 10101
which is placed in the caller program at call position.
⮚ On executing the caller, program control gets transferred in the required address to invoke
the calculate() function.
== x ==