0% found this document useful (0 votes)
119 views

Oops (Object Oriented Programming Structure)

OOPs (Object Oriented Programming Structure) uses concepts like class, encapsulation, inheritance, and polymorphism to model real-world objects in code. A class acts as a blueprint for an object, defining its properties and functions. Encapsulation groups an object's properties and behaviors into a single class. Inheritance allows a child class to inherit properties from a parent class and add new properties. Polymorphism allows the same function to behave differently depending on the object it is called on.

Uploaded by

MANOJ S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Oops (Object Oriented Programming Structure)

OOPs (Object Oriented Programming Structure) uses concepts like class, encapsulation, inheritance, and polymorphism to model real-world objects in code. A class acts as a blueprint for an object, defining its properties and functions. Encapsulation groups an object's properties and behaviors into a single class. Inheritance allows a child class to inherit properties from a parent class and add new properties. Polymorphism allows the same function to behave differently depending on the object it is called on.

Uploaded by

MANOJ S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OOPs ( Object Oriented Programming Structure )

1. OOPs :
OOPs is just a technique which has some concepts for Objects . Before going to
know about those concepts we need to understand what an Object is and Why we go for
OOPs technique .

2. Object :
In computer terms we call every real world entity as an object . In the real world ,
everything ( laptop , human , animal , trees , etc… ) has its own property and
functionality .
● Ex :
○ Object : Human
○ Properties : Eyes, nose , mouth , fingers
○ Functionalities : Watching , Eating , Writing

Hence If we create an application for a real world entity we should handle that
object’s properties and functionalities in an efficient manner to get an optimized
application . That’s why we are going for OOPs technique.

3. Important concepts in OOPs :


OOPs have some important concepts for Objects to handle those properties and
functionalities.
● Class
● Encapsulation
● Inheritance
● Polymorphism
● Data Abstraction

3.1 Class :
Class is a programming part where we can define properties and functionality of
an object in a single place . It will act as a template ( like blueprint in construction ) for
the object . We can create objects using this template .
Real world Ex : In construction we have a blueprint and using that we can build
any number of buildings with the same property .
Like here we can create any number of objects with the same property using that
class template .
Inside class we can define constructor and destructor .
Constructor is used to create objects and a destructor is used for destroying
objects.
Code :

class Student {

// properties
string name;
int id ;
string dep;

//functionality
void writing() {}
void reading() {}

Example 1

3.2 Encapsulation :

Encapsulation is a process of clubbing both property ( data ) and functionality


( behavior ) of an object in a single entity ( class ) . Encapsulation is the basic and
important concept of OOPs technique .
Ex : In the above example 1.1 , The property and functionality of the student
object clubbed under the Student class .

3.3 Inheritance :

In the real world most of the things Inherit properties from another thing and
additional to that it has its own property .
● Ex : Son is getting some behaviors from his parents like eating, breathing,
walking .
In those situations in application we do not need to rewrite parent class behavior to
the child the child class . We can just extend the parent class in the child class .

Code :

class Parent {

// properties
Eyes
Nose
Mouth

//functionality
eating();
sleeping();
walking();
breathing();

class Child extends Parent {

// properties
Eyes //Getting automatically from Parent
Nose //Getting automatically from Parent
Mouth //Getting automatically from Parent

//functionality
eating(); //Getting automatically from Parent
sleeping(); //Getting automatically from Parent
walking(); //Getting automatically from Parent
breathing(); //Getting automatically from Parent
painting();
singing();

}
Example 2

3.3.1 Types of Inheritance :


Inheritance is divided into 5 types ,
● Single Inheritance
● Multiple Inheritance
● Multi Level Inheritance
● Hierarchical Inheritance
● Hybrid Inheritance

3.3.1.1 Single Inheritance :


One Child inheriting properties from one parent class is called Single
Inheritance .

3.3.1.2 Multiple Inheritance :


One Child inheriting properties from more than one parent is called Multiple
Inheritance .

3.3.1.3 Multi Level Inheritance :


One child inheriting properties from another class which already inherits
properties from another class is called Multi level Inheritance .

3.3.1.4 Hierarchical Inheritance :


More than child classes inherit property from one common parent class is
called Hierarchical Inheritance .

3.3.1.5 Hybrid Inheritance :


The process of combining more than one of the above mentioned
inheritances is known as Hybrid Inheritance .

3.4 Polymorphism :
Polymorphism means having many forms . For example a woman can be a sister ,
mother , teacher in different places . like that the object's behavior can be different in
different places .
● It has two different categories :
○ Compile Time polymorphism .
○ Runtime polymorphism .

What is compile time ? :- Compiling is the process of checking syntax and


semantics . This state can be called compile time .

What is run time ? :- After the successful compilation , the program will be
executed and that state is called runtime .

3.4.1 Compile time polymorphism :


The polymorphism which happens while compiling is called Compile Time
polymorphism . It can also be known as early binding or Static polymorphism. It can be ,
● Function overloading
● Operator overloading // Not available in java
● Constructor overloading

3.4.1.1 Function overloading :


Function overloading is the process of overloading a function . The rules are ,
● The function name should be the same .
● The operant data types or number of operants should be different .
Example : We can use the area function for calculating the area of Rectangle or
Square or Circle .

Code :

class Area {
int area(int a ) // area of square
{
return a*a;
}
int area(int l,int m) // area of rectangle
{
Return l*m;
}
float area(float r) // area of circle
{
return 3.14*r*r;
}
}
Example 3

Explanation of the code :


● In the above example , if we call an area function like this area(2) then it
will invoke a single int argument area function ( area for square ) .
● If we call like area(2,3) then the area for the rectangle function will be
invoked.
● If we call an area function like area(2.34f) then the area function of Circle
will be invoked .

3.4.1.2 Operator Overloading :


Operator overloading is the process of overloading an operator . i.e, operators
behavior will be vary based on the operands.
Ex : ‘+’ symbol will be acting as an additional operator if the both operators are
primitives . If any one operant is string then the additional operator will be acting as a
concatenation operator.
Note : Java doesn’t allow operator overloading . Because it may cause confusion .

3.4.1.3 Constructor Overloading :


What is a constructor ?
Constructor are commonly used for Initializing objects .
Constructor name is the same as the class name .
Constructor should not have any return type . void also not allowed .
Constructors also can be overloaded like other methods .

Ex :

class Student
{
int id ;
int name ;
Student(){
id=0;
name=””;
}
Student(int id1)
{
id=id1;
name=””;
}
Student(int id1,string name1)
{
id=id1;
name=name1;
}
}

Example 4

3.4.2 Runtime Polymorphism


The Polymorphism which is happening while run time is called Runtime
polymorphism . It can also called as late binding or dynamic polymorphism .It can be
categorized as ,
● Function Overriding
● Virtual Function

3.4.2.1 Overriding
Method Overriding is a polymorphism which happens between child and parent
classes. In Method Overriding the method signature should be matched . In Overriding ,
the parent class method is always overridden by the child class method in child class .
Method overriding is based on the underlying object not based on reference.
Example :

class parent {
void print(){
System.out.println("parent class");
}
}
class child extends parent{
void print(){
System.out.println("Child class");
}
}
class Main{
public static void main(String[] args)
{
child c=new child();
c.print(); // child class
}
}

Example 5

We can get parent class specific methods using parent class objects .

3.4.2.2 Virtual Function


Virtual is a keyword in c++. This keyword is used to make virtual functions. Even
If both parent and child classes have the same functions with the same signature , if we
call child objects function using parent class reference then parent class function only
will be invoked . If we make the parent function virtual then the function will be invoked
based on the object’s type not reference type . It is also called object slicing .

Example :

#include<iostream>
using namespace std;
class parent{
public:
virtual void print(){
cout<<"hello";
}
};
class child : public parent{
public:
virtual void print3(){
cout<<"hai";
}
};
int main()
{
parent* p;
child c;
p=&c;
p->print();
}

Example 6

Process flow of the above Example :


In the above example , the function call will invoke the parent class function . In
the parent class we declare the function as virtual. Hence the compiler will check any
available methods in the object’s class. If anything is found then that function will be
invoked else the parent class function will be executed .

3.5 Data Abstraction :


Data abstraction is the process of hiding certain details and showing only
important details . It is one of the most important and great features of the OOP
language . Because it provides security by hiding sensitive data to the outer world .
Example : If we want to verify our biometric data with our aadhar data , It will
take our biometric information and notify us that both are matched or not . We can’t
access the data stored in aadhaar database directly and we can’t get to know how the
comparison is implemented . By hiding the data and implementation the data security
will be improved .
Data Abstraction can be achieved by public , private and protected in C++ .

3.5.1 public
If we declare a variable or function under public , then we can access those
variables or functions anywhere in the program using the object reference .
It provides program level access .

3.5.2 private
If we declare a variable of a function under private, then we can access those
variables or functions inside the same class only .
It provides class level access .

3.5.3 protected
If we declare a variable of a function under protected then we can access those
variables or functions inside the same class or Its child class .

Data abstraction can also be used to optimize the code without exposing the
internal implementation . Ex : In MS-DOS they are using only 16 bits for storing DATE
information .

You might also like