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

Lec 03_OOPs Concepts

Uploaded by

right2abhinavrs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lec 03_OOPs Concepts

Uploaded by

right2abhinavrs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

23CSE111 Object Oriented Programming

Lecture 2

Object Oriented Concepts

Dr Gowtham Ramesh
What is object-oriented
programming (OOP)?
• Object-oriented programming is a method of
implementation in which programs are organized as
cooperative collections of objects, each of which
represents an instance of some class, and whose
classes are all members of a hierarchy of classes united
via inheritance relationships.
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence
Abstraction

• Abstraction is one of the fundamental ways that we as


humans cope with complexity

Domain Name of the


Website
Abstraction

• Abstraction is one of the fundamental ways that we as


humans cope with complexity
Your car is a great example of
abstraction
Abstraction
• Recognition of similarities between certain objects, situations, or
processes in the real world
Abstraction - Definition
• An abstraction denotes the
essential characteristics of
an object that distinguish it
from all other kinds of
objects and thus provide
crisply defined
conceptual boundaries,
relative to the perspective of
the viewer.
Abstraction – In CS

Class = State+ Behaviour


Class = State+ Behaviour
 State
data members
fields
properties

 Behaviour
member functions
methods
Abstraction – Class

Class: It is a user-defined data type, which


holds its own data members and member
functions to work on that data.

Data Members – Attributes Eg., Fuel


Member Functions – Methods Eg., refuel()
Abstraction – Object
Abstraction – Object
Object
• An Object is an identifiable entity with
some characteristics and behavior
• An Object is an instance of a Class
• An object has state, exhibits some well
defined behaviour
• When a class is defined, no memory is
allocated but when it is instantiated (i.e.
an object is created) memory is allocated.
Class = State+ Behaviour

• The state of an object encompasses


all of the properties of the object
plus the current values of each of
these properties.

• Behavior is how an object acts and


reacts, in terms of its state changes
and message passing.
Abstraction – Objects in Real World
Abstraction – Objects in Real World
Abstraction – Objects in Real World
Abstraction – In Nutshell
Abstraction
Two Types:

• Data Abstraction - When the object data is not visible to the outer world, it creates data
abstraction. If needed, access to the Objects’ data is provided through some methods.

• Process Abstraction - We don’t need to provide details about all the functions of an object.
When we hide the internal implementation of the different functions involved in a user
operation, it creates process abstraction.
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence
Encapsulation
Encapsulation
• Encapsulation refers to the bundling of data with the
methods that operate on the data.
• It may also refer to the limiting of direct access to
some of that data, such as an object's components.
• Essentially, encapsulation prevents external code from
being concerned with the internal workings of an
object.
• Therefore, encapsulation can be used to hide data
members and functions associated with an instantiated
class or object.
Encapsulation
• FIRST LAW OF OOP: Data must be
hidden, i.e.,PRIVATE
• Read access through read functions
• Write access through write functions
• For every piece of data, 4 possibilities
 read and write allowed
 read only
 write only
 no access
Encapsulation
• FIRST LAW OF OOP: Data must be
hidden, i.e.,PRIVATE
• Read access through read functions
• Write access through write functions
• For every piece of data, 4 possibilities
 read and write allowed
 read only
 write only
 no access
Objects – Public Interface
Abstraction and Encapsulation
• Abstraction focuses on the observable behavior of an object, whereas
encapsulation focuses on the implementation that gives rise to this
behavior.
• For abstraction to work, implementations must be encapsulated
• Each class must have two parts: an interface and an implementation
• Examples Pls....!
Lifecycle of an Object

Born healthy Special Functions –


CONSTRUCTORS​ ensure correct
using constructors
initialization of all data,
 Lives Safely automatically called at the time of
using read/write functions
object creation.
 Dies cleanly
using destructors
Lifecycle of an Object

Born healthy Special Functions –


DESTRUCTORS ensure
using constructors
correct Deallocation of all
 Lives Safely resources before an object
“dies” (goes out of scope).
using read/write functions
 Dies cleanly
using destructors
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence
The Hierarchy
• We may find many more different abstractions than we can
comprehend at one time
• A set of abstractions often forms a hierarchy
• Identifying these hierarchies in our design, we greatly simplify
our understanding of the problem
Hierarchy is a ranking or ordering of abstractions
• The two most important hierarchies
“is a” hierarchy - hierarchies in the class structure
"has a" or "Part of" - hierarchy in the object structure
Inheritance
• Inheritance is the most important “is a” hierarchy
Inheritance is the most important “is a” hierarchy
Inheritance

• Structure and behavior that are common for


different classes will tend to migrate to
common superclasses
• Inheritance is being a generalization
/specialization hierarchy
• Superclasses represent generalized abstractions
• Subclasses represent specializations in which
fields and methods from the superclass are
added, modified, or even hidden.
Inheritance - Examples
Animal

Vertebrates Invertebrates

Mammal Fish Reptile Birds

Apes Monkeys Ungulates

Gorillas Chimpanzees Humans


Inheritance - OO Example
Inheritance - Nomenclature

Not recommended BEST

Superclass Parent class Base class

Subclass Child class Derived class


Inheritance - Types
Inheritance – Multiple Inheritance
Inheritance – Multiple Inheritance

• MI introduce some practical complexities for


programming languages

• Languages must address two issues


 Clashes among names from different
superclasses
 Repeated inheritance
Inheritance - Ambiguity in Multiple
Inheritance
A
Horse
B C • Graphs have more complicated Eagle
eat()
structure than trees. eat()

• More difficult to maintain and fix


F E D bugs etc.
Flying Horse
• Ambiguity in function calls.
G eat()?

Graph Structure of Multiple Inheritance


Without Inheritance?
Is Inheritance violating Encapsulation?
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence
Modularity
• The act of partitioning a program into individual
components can reduce its complexity to some
degree.
• creates a number of well-defined, documented
boundaries within the program

• Modularity packages abstractions into discrete


units
• Especially for larger applications, in which we
may have many hundreds of classes, the use of
modules is essential to help manage complexity
Modularity
• Modularization consists of dividing a program
into modules which can be compiled separately,
but which have connections with other modules
• The cost of recompiling the body of a module is
relatively small
• Classes, Packages, Domains
Example:
Let's consider a multimedia application that deals with
different types of media files such as images, videos,
and audio files. We can create separate classes for each
type of media file, each encapsulating its own
functionality:
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence
Strong Typing

• Strong typing prevents mixing abstractions.


X=Y
• is allowed only if X and Y are objects of the
same class.
• Weak typing: Allow some violations
• Untyped: Dynamic Typing (LISP, javascript,
etc.)
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence
Concurrency

• Concurrency allows different objects


to act at the same time.

• Java’s support of multithreading is a


form of concurrency.
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence
Persistence

• Persistence saves the state and


class of an object across time and
space, i.e., storage on permanent
storage media.

Object Serialization is supported in


Java
Elements of OOP Model

Major elements of OOP model:


• Abstraction
• Encapsulation
• Inheritance
• Modularity
Minor elements of OOP model:
• Strong Typing
• Concurrency
• Persistence

You might also like