UNIT1- OBJECT ORIENTED PROGRAMMING IN JAVA- AIML IT-SPPU

ApurvaLaddha 174 views 24 slides Sep 10, 2024
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

IT IS THE PRESENTATION OF FIRST UNIT OF OBJECT ORIENTED PROGRAMMING IN JAVA OF AIML DEPARTMENT


Slide Content

Software evolution
•Process of continually developing and updating
software to meet the requirements of the
market over time.
•Upgrading the system improves usability and
functionality.
•When the software is upgraded, it is stable
enough to decrease security risks and undergo
cyber attacks over time

Introduction to Procedural Programming

•It can be defined as a programming model which is
derived from structured programming, based upon
the concept of calling procedure.
•Procedures, also known as routines, subroutines or
functions, simply consist of a series of computational
steps to be carried out.
•During a program’s execution, any given procedure
might be called at any point, including by other
procedures or itself.
•Example of procedural programming is C language,

Object-oriented programming
•It can be defined as a programming model which is based
upon the concept of objects.
•Objects contain data in the form of attributes and code in
the form of methods.
• In object-oriented programming, computer programs are
designed using the concept of objects that interact with
the real world.
•Object-oriented programming languages are various but
the most popular ones are class-based, meaning that
objects are instances of classes, which also determine
their types.

Procedural Oriented
Programming
Object-Oriented Programming
In procedural programming, the
program is divided into small parts
called functions.
In object-oriented programming, the
program is divided into small parts
called objects.
Procedural programming follows
a top-down approach.
Object-oriented programming follows
a bottom-up approach.
There is no access specifier in
procedural programming.
Object-oriented programming has
access specifiers like private, public,
protected, etc.


In procedural programming,
overloading is not possible.
Overloading is possible in
object-oriented programming.
In procedural programming, there
is no concept of data hiding and
inheritance.
In object-oriented programming, the
concept of data hiding and inheritance
is used.

Procedural programming is
based on the unreal world.
Object-oriented programming is
based on the real world.
Procedural programming is used
for designing medium-sized
programs.
Object-oriented programming is
used for designing large and
complex programs.

Code reusability absent in
procedural programming,
Code reusability present in
object-oriented programming.
Examples: C, FORTRAN,
Pascal, Basic, etc.
Examples: C++, Java, Python, C#,
etc.

Limitations of procedure oriented
programming

1.Procedural programming mainly focuses on procedures or
functions. Less attention is given to the data.
2.The data and functions are separate from each other.
3.Global data is freely moving and is shared among various functions.
Thus, it becomes difficult for programmers to identify and fix issues
in a program that originate due to incorrect data handling.
4.Changes in data types need to be carried out manually all over the
program and in the functions using the same data type.
5.Limited and difficult code reusability.
6.It does not model real-world entities (e.g., car, table, bank account,
loan) very well where we as a human being, perceive everything as
an object.
7.The procedural programming approach does not work well for
large and complex systems.

Why do we need Object Oriented Programming?
1.Code Organization and Reusability : With OOP, code can be
modularized, promoting reusability and reducing redundancy.
2.Modularity and Scalability : OOP promotes modularity, breaking
down complex systems into smaller, manageable
components. This modular approach also enables scalability,
as new objects can be added or modified without affecting the
entire system.
3.Encapsulation and Data Security: Encapsulation protects data
integrity by allowing controlled access to the internal state of
an object. This enhances data security and reduces the risk of
unintentional data manipulation, as objects control their own
data and expose only necessary interfaces to other objects.

4.Inheritance and Code Reuse: OOP supports inheritance,
where new classes can inherit properties and behaviors
from existing classes. Inheritance facilitates code reuse,
as common functionalities can be defined in a base class
and inherited by derived classes. This reduces code
duplication, improves code organization, and enhances
maintainability.

5.Collaboration and Teamwork: OOP allows multiple
developers to work concurrently on different parts of a
system without conflicts. This promotes teamwork,
simplifies code integration, and streamlines the
development process.

Fundamentals of OOP
•Object Oriented Programming is the most recent concept among programming
model. The motivating factor in the invention of object oriented approach is to
remove some of the flows encountered in the procedural approach.
•Object-Oriented Programming or OOPs refers to languages that use objects in
programming.
•Object-oriented programming aims to implement real-world entities in programming.

•The main aim of OOP is to bind together the data and the functions that operate on
them so that no other part of the code can access this data except that function.

•Some of the striking features of Object Oriented Programming are:
1. Importance on data rather than procedure.
2. Programs are divided into what are known as objects

Characteristics of OOPs are :

•Object
•Classes
•Polymorphism
•Inheritance
•Reusability
•Data abstraction and encapsulation
•Dynamic Binding
•Message Passing

1. Class:
A class is a user-defined data type.
It consists of data members and member functions,
For Example: Consider the Class of Cars. There may be many cars with different names and brands
but all of them will share some common properties like all of them will have 4 wheels. Speed Limit,
Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage are their properties.

2. Object:
It is a basic unit of Object-Oriented Programming and represents the real-life entities.
An Object is an instance of a Class.
When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created)
memory is allocated.
An object has an identity, state, and behavior. Each object contains data and code to manipulate the
data.
For example: “Dog” is a real-life Object, which has some characteristics like color, Breed, Bark,
Sleep, and Eats.

Data Abstraction:
Data abstraction refers to providing only essential
information about the data to the outside world, hiding
the background details or implementation.
Consider a real-life example of a man driving a car.
The man only knows that pressing the accelerators will
increase the speed of the car or applying brakes will stop
the car, but he does not know about how on pressing the
accelerator the speed is increasing,
he does not know about the inner mechanism of the car
or the implementation of the accelerator, brakes, etc in
the car. This is what abstraction is.

Encapsulation:
•Encapsulation is defined as the wrapping up of data under a
single unit.
• It is the mechanism that binds together code and the data it
manipulates.
•In Encapsulation, the variables or data of a class are hidden
from any other class and can be accessed only through any
member function of their class in which they are declared.
•As in encapsulation, the data in a class is hidden from other
classes, so it is also known as data-hiding.

Example of Encapsulation
•Consider a real-life example of encapsulation, in a company, there are
different sections like the accounts section, finance section, sales
section, etc.
• The finance section handles all the financial transactions and keeps
records of all the data related to finance.
•Similarly, the sales section handles all the sales-related activities and
keeps records of all the sales.
• Now there may arise a situation when for some reason an official
from the finance section needs all the data about sales in a particular
month.
• In this case,
•he is not allowed to directly access the data of the sales section. He
will first have to contact some other officer in the sales section and
then request him to give the particular data.
•This is what encapsulation is. Here the data of the sales section and
the employees that can manipulate them are wrapped under a single
name “sales section””.

Inheritance
•Inheritance is an important pillar of
OOP(Object-Oriented Programming).
•The capability of a class to derive properties and
characteristics from another class is called Inheritance.
•When we write a class, we inherit properties from other
classes. So when we create a class, we do not need to
write all the properties and functions again and again, as
these can be inherited from another class that possesses
it.
•Inheritance allows the user to reuse the code whenever
possible and reduce its redundancy.

Java Inheritance Example
Programmer is the subclass and Employee is the superclass. The relationship
between the two classes is Programmer IS-A Employee. It means that Programmer is
a type of Employee.

class Employee{
float salary=40000;
}

class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

output= Programmer salary is:40000.0
Bonus of programmer is:10000

Types of Inheritance

Polymorphism
• The word polymorphism means having many forms.
•In simple words, we can define polymorphism as the
ability of a message to be displayed in more than one
form.
For example,
A person at the same time can have different
characteristics. Like a man at the same time is a father, a
husband, an employee. So the same person posses
different behavior in different situations. This is called
polymorphism.

Types of Polymorphism

Dynamic binding
•In dynamic binding, the code to be executed in
response to the function call is decided at runtime.
•Dynamic binding means that the code associated
with a given procedure call is not known until the
time of the call at run time.
•Dynamic Method Binding One of the main
advantages of inheritance is that some derived class
D has all the members of its base class B. Once D is
not hiding any of the public members of B, then an
object of D can represent B in any context where a
B could be used.
•This feature is known as subtype polymorphism.

Example for dynamic binding
class India {
public void locate() {
System.out.println("We are in India");
}
}
class SubIndia extends India {
public void locate() {
System.out.println("We are in Delhi");
}
}
public class DynamicBindingTest {
public static void main(String args[]) {

// Method of super class or parent class is called
India superobj = new India();
superobj.locate();

// Method of sub class or child class is called
by Parent reference,
// this is called "Dynamic Binding"

India subobj = new SubIndia(); // upcasting
subobj.locate();

SubIndia subobj2 = new SubIndia();
subobj2.locate(); // method of sub class or
child class is called
}
}

Output :
We are in India
We are in Delhi
We are in Delhi
Explanation :
In this code, there is a superclass called India that has a method
called locate(), which simply prints "We are in India". The
subclass SubIndia extends India and overrides
the locate() method to print "We are in Delhi".
In the main() method, an object of India is created and
its locate() method is called, which prints "We are in India".
Then, an object of SubIndia is created and upcast to the type
of India. When the locate() method is called on this object using
the India reference variable, dynamic binding comes into play
and the overridden locate() method in the SubIndia class is
called, printing "We are in Delhi". This is because the actual
type of the object is SubIndia, and so its implementation of
the locate() method is used.
Finally, an object of SubIndia is created directly and
its locate() method is called, which again prints "We are in
Delhi".

Message Passing
•It is a form of communication used in object-oriented
programming as well as parallel programming.
• Objects communicate with one another by sending
and receiving information to each other.
•A message for an object is a request for execution of a
procedure and therefore will invoke a function in the
receiving object that generates the desired results.
• Message passing involves specifying the name of the
object, the name of the function, and the information to
be sent.

•For example, when Object A invokes a public method of
Object B, it is sending a message to Object B. Note that
Object A can only invoke Object B's public methods, not
its private methods.
Tags