Unit1 - OOP and Demo
Unit1 - OOP and Demo
Unit 1
Overview of the Unit
int x X
Not a X= 45 X 45 String
primitive Object
data type Stringstr; str = “Java”
1050 Java
str = new Str 1050
String(“Java”);
String Instances
Reference Operato
r Object of Class
Variable
String
Hence..
While working with classes
larger(5, 3)
larger(‘A’, ‘9’),
Classes and instances
Class is an extensible program-code template for
Creating objects,
Providing initial values for state
Implementations of behavior
When a constructor creates an object, that
object is calledan instance of the class.
The creation of an instance is calledinstantiation.
Objects
Object : CUSTOMER Object : ACCOUNT
DATA DATA
AC No. AC No.
Name of AC Holder AC Balance
Address Type of Account
FUNCTIONS FUNCTIONS
Deposit Account Balance
Withdrawal
AC Balance Display
Basic Concepts of OOP
Classes
Classes are user-defined data types.
The entire set of data and code of an object can be made a user-defined data
type with the help of a class. Objects are variables of the type class. Once
a class has been defined, we can create any number of objects belonging
to that class. Each object is associated with the data of type class with
which they are created.
Classes
fruit mango;
Attributes: Attributes:
------------ ------------
------------ ------------
Object A Object B
Data Data
Communication
Functions Functions
Object C
Data
Functions
CHARACTERISTICS OF OBJECT-ORIENTED
PROGRAMMING
OUTPUT
METHODS
Methods are functions defined inside the body of a class. They are used to define the
behaviors of an object.
METHODS
Methods are functions defined inside the body of a class. They are used to define the
behaviors of an object.
INHERITANCE
Inheritance is a way of creating new class for using details of existing class without
modifying it. The newly formed class is a derived class (or child class). Similarly, the
existing class is a base class (or parent class).
SINGLE INHERITANCE
PRIVAT
E
UPDATE
EXTEND
INHERI
T
DATA ENCAPSULATION IN PYTHON
POLYMORPHISM
PARAMETRIZED CONSTRUCTOR
DESTRUCTORS IN PYTHON
Destructors are called when an object gets destroyed. In Python, destructors are not needed as
much needed in C++ because Python has a garbage collector that handles memory management
automatically. The __del__() method is a known as a destructor method in Python.
Python - public, private and protected Access
Modifiers
All members in a Python class are public by default.
Any member can be accessed from outside the class
environment.
Example: Public Attributes
class employee:
def __init__(self, name, sal):
self.name=name
self.salary=sal
Python's convention to make an instance variable
protected is to add a prefix _ (single underscore) to
it. This effectively prevents it to be accessed, unless it
is from within a sub-class.