0% found this document useful (0 votes)
4 views40 pages

Object-Oriented Programming Concepts

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views40 pages

Object-Oriented Programming Concepts

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CST 205 OOP :OBJECT

ORIENTED CONCEPTS
Prepared By,
[Link] Radhakrishnan
OBJECTIVES
▪ To explain about object oriented concepts.
▪ To illustrate object oriented design principles.

2
Basic Object Oriented concepts
▪ Object means a real-world entity
such as a pen, chair, table,
computer, watch, etc
▪ Object-Oriented Programming is a
methodology or paradigm to design a
program using classes and objects
▪ The main aim of object-oriented
programming is to implement real-
world entities

3
Basic Object Oriented concepts
Object
✔ Any 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.
✔ They are the combination of data & logic that represents some
real world entity.
✔ Objects are the basic run‐time entities of an object oriented
system.
4
Basic Object Oriented concepts
Object
✔ When a program is executed, the objects
interact by sending messages to one another.
✔ Each object contains data, and code to manipulate the data.
✔ A dog is an object because it has states like color, name, breed,
etc. as well as behaviors like wagging the tail, barking, eating,
etc.

5
Basic Object Oriented concepts
Object
▪ The data of the object can only be accessed by the methods of the object.
▪ Consequently, the only access point to the data for the external Objects is
through the invocation of the methods of the object.
▪ No object can directly access the data of any other object.
▪ An object can access the private data of another object by invoking the
methods supported by that object.
▪ This mechanism of hiding data from other objects is popularly known as the
principle of data hiding or data abstraction
▪ The data stored internally in an object are called its attributes, and the
operations supported by an object are called its methods.

6
Basic Object Oriented concepts

7
Basic Object Oriented concepts
CLASS
▪ Collection of objects is called class.
▪ CLASS are a blueprint or a set of instructions to build a specific type of
object.
▪ An object is an instance of a class.
▪ Class in Java determines how an object will behave and what the object
will contain.
▪ Let’s take Human Being as a class. My name is John, and I am an
instance/object of the class Human Being.
▪ Object has a physical existence while a class is just a logical definition.

8
Basic Object Oriented concepts

9
CLASS
▪ Car is the class and wheels, speed limits, mileage are their
properties.
▪ A Class is a user defined data-type which has data members and
member functions.
▪ Data members are the data variables and member functions are the
functions used to manipulate these variables and together these
data members and member functions defines the properties and
behavior of the objects in a Class.
▪ In the above example of class Car, the data member will be speed
limit, mileage etc and member functions can be apply
brakes, increase speed etc.

10
Basic Object Oriented concepts

11
Understand the concept of Java Classes and
Objects with an example.

12
Understand the concept of Java Classes and
Objects with an example.

Class - Dogs
Data members or objects- size, age, color, breed,
etc.
Methods- eat, sleep, sit and run. 13
Understand the concept of Java
Classes and Objects with an example.

14
What are the four basic principles/ building
blocks
of OOP (object oriented programming)?
▪ Inheritance
▪ Encapsulation
▪ Abstraction
▪ Polymorphism

15
Basic Object Oriented concepts
Inheritance
▪ The capability of a class to derive properties and characteristic from
another class is called Inheritance.
▪ Sub Class : The class that inherits properties from another class
is called Sub class or Derived Class.
▪ Super Class : The class whose properties are inherited by sub
class is called Base Class or Super class.
▪ The subclass can add its own fields and methods in addition to the
superclass fields and methods.
▪ It supports the concept of hierarchical classification.
16
Basic Object Oriented concepts
Inheritance :- Reusability:
▪ Inheritance supports the concept of “reusability”,i.e. when we want to
create a new class and there is already a class that includes some of
the code that we want, we can derive our new class from the existing
class. By doing this, we are reusing the fields and methods of the
existing class.

17
Basic Object Oriented concepts - Inheritance

18
Basic Object Oriented concepts
Inheritance :-
▪ Each derived class can be considered as a specialisation of its base
class because it modifies or extends the basic properties of the base
class in certain ways.
▪ Therefore, the inheritance relationship can be viewed as a
generalisation-specialisation relationship.
▪ In addition to inheriting all the data and methods of the base class, a
derived class usually defines some new data and methods.
▪ A derived class may even redefine a method which already exists in
the base class.

19
Generalization

20
Basic Object Oriented concepts
Advantages of inheritance
⮚ Reusability: facility to use methods of base class with out rewriting the
same.
⮚ Data hiding: Using access specifiers, base class can decide to keep
some data private so that it cannot be altered by the derive class.
⮚ Extensibility: extending the base class logic.
⮚ Through effective use of inheritance, you can save lot of time in your p
rogramming and also reduce errors,
which in turn will increase the quality of work and productivity.
.

21
Basic Object Oriented concepts
Encapsulation
▪ It is the process of binding both attributes and
methods together in a single unit , as a class.
▪ Through encapsulation, the internal details of a class
can be hidden from outside.
▪ It permits the elements of the class to be accessed
from outside only through the interface provided by
the class.
22
Basic Object Oriented concepts
▪ Encapsulation
Binding (or wrapping) code and data together into a
single unit are known as encapsulation
For example: consider a Medical store. Lets say you have to buy
some medicines. You go to the medical store and ask the chemist for the medicines.
Only the chemist has access to the medicines in the store based on your prescription .Chemist
knows what medicines to give to you.
▪ In this example
▪ MEDICAL STORE: class
▪ MEDICINES: Member Variables.
▪ CHEMIST: Member Methods.
▪ You: External Application or piece of Code accessing members with method in class
23
concepts :Encapsulation
▪ Through Encapsulation, Data is not accessible to
the outside world, and only those functions which are wrapped in the
class can access it.
▪ In encapsulation, the variables of a class will be hidden from other
classes and can be accessed only through the methods of their current
class. Therefore, it is also known as data hiding.

▪ To achieve encapsulation in Java −


▪ Declare the variables of a class as private for data hiding.
▪ Provide public setter and getter methods to modify and view the
variables values.
24
Basic Object Oriented
concepts :Encapsulation

25
Basic Object Oriented
concepts :Encapsulation
▪ Data Hiding: It is a way to achieve data hiding in Java because
other class will not be able to access the data through the private data
members.
▪ Protection of data from accidental corruption,by means of access
specifiers (Private/Public),it provides security.
▪ Encapsulation protects an object from unwanted access by clients.
▪ Increased Flexibility: We can make the variables of the class as
read-only or write-only depending on our requirement.
▪ Reduction in complexity.

26
Basic Object Oriented concepts
Abstraction
⮚ Data Abstraction is the property by virtue of which only the
essential details are displayed to the user.
⮚ Data Abstraction may also be defined as the process of
identifying only the required characteristics of an object
ignoring the irrelevant details.
⮚ In other words, the user will have the information on what the
object does instead of how it does it.
⮚ The trivial or the non-essentials units are not displayed to the
user. Ex: A car is viewed as a car rather than its individual
components. 27
Basic Object Oriented concepts
Abstraction
⮚ Consider a real-life example of a man driving a car.
⮚ The man only knows that pressing the accelerators will increase the speed of
car or applying brakes will stop the car but he does not know about how on
pressing the accelerator the speed is actually increasing, he does not know
about the inner mechanism of the car or the implementation of accelerator,
brakes etc in the car.

⮚ In java, abstraction is achieved by interfaces


and abstract classes.

28
ABSTRACTION VS ENCAPSULATION

29
Basic Object Oriented concepts
POLYMORPHISM
⮚ The word polymorphism means having many forms. Poly means
many and morph means form; which means objects can take
many different forms .

⮚ Suppose if you are in class room that time you behave like a student,
when you are in market at that time you behave like a customer, when
you at your home at that time you behave like a son or daughter, Here
one person present in different-different behaviors.

⮚ An operation may exhibit different behaviours in different instances.


The behavior depends upon the types of data used in the operation.
30
Basic Object Oriented concepts
POLYMORPHISM

31
Basic Object Oriented concepts
POLYMORPHISM
An operation may exhibit different behaviours in different
instances. The behavior depends upon the types of data used in
the operation.

32
Basic Object Oriented concepts
⮚ How to achieve Polymorphism in Java ?
⮚ In java programming the Polymorphism principal is
implemented with method overriding concept of java.
⮚ Polymorphism principal is divided into two sub principal they
are:
⮚ Static or Compile time polymorphism
⮚ Dynamic or Runtime polymorphism
⮚ Static polymorphism in Java is achieved by method overloading
and Dynamic polymorphism in Java is achieved by method
overriding.
33
Basic Object Oriented concepts

34
Basic Object Oriented concepts
▪ Method overloading allows methods that perform similar or closely related
functions to be accessed through a common name. Method overloading allows
you to define any number of methods with the same name and different types
of parameters to handle the array of operations.
▪ Method overriding allows a sub class to use all the general definitions that a
super class provides and add specialized definitions through overridden
methods.
▪ Method overriding works together with inheritance to enable code reuse
of existing classes without the need for re-compilation.
▪ It helps the programmer to reuse the codes.

35
Basic Object Oriented concepts

36
Benefits of OOD
▪ User can create new user defined data type by making class.
▪ Data can be encapsulated from outside world .
▪ By using polymorphism, same functions or operators can be used for
multitasking.
▪ Object oriented system can be easily upgrade from small system to large
system.
▪ Software complexity can be easily managed.
▪ Maintenances cost is less.
▪ Code reuse by the use of predeveloped class libraries.
▪ Code reuse due to inheritance
▪ Simpler and more intuitive abstraction, i.e., better management of inherent
problem and code complexity
▪ Better problem decomposition. 37
Disadvantages of OOD
▪ The principles of abstraction, data hiding, inheritance, etc. do incur run time
overhead due to the additional code that gets generated on account of
these features. This causes an object-oriented program to run a little slower than
an equivalent procedural program.

▪ An important consequence of object-orientation is that the data that is


centralised in a procedural implementation, gets scattered across various
objects in an object-oriented implementation. Therefore, the spatial locality of
data becomes weak and this leads to higher cache miss ratios and consequently
to larger memory access times. This finally shows up as increased program run
time.

38
Summary
• Object means a real-world entity such as a pen, chair, table,
computer, watch, etc.
• Object-Oriented Programming is a methodology or paradigm
to design a program using classes and objects.
• It simplifies software development and maintenance by providing
some concepts:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation 39
Thanks!
Any questions?

40

You might also like