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

Unit 4

Uploaded by

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

Unit 4

Uploaded by

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

UNIT IV

OBJECT ORIENTED PROGRAMMING


Unit - IV

Defining classes, self parameter and adding methods to a class, The


_init_ Method,Inheritance, Types of inheritance, super().
Object Oriented Programming Evolution

● Express the task in terms of programming language


● Evolution
–Algorithm
–Assembly Language 1950’s
–High level Language 1960’s(Fortran, Cobol, Basic)
–System Programming 1970’s ( C )
–Object Oriented Programming 1980’s (C++)
–Scripting, Web, Component based, Framework 1990’s(Java, python,VB, Javascript)
Programming paradigms : Python

● Functional
● Procedural
● Object oriented
Object oriented programming
Python Classes and Objects

● A class is a user-defined blueprint or prototype from which objects are


created.
● Classes provide a means of bundling data and functionality together.
● Creating a new class creates a new type of object, allowing new instances of
that type to be made.
● Each class instance can have attributes attached to it for maintaining its
state.
● Class instances can also have methods (defined by their class) for
modifying their state.
Class and Object Definition



Object of Python Class

An Object is an instance of a Class. A class is like a blueprint while an instance is a


copy of the class with actual values.

An object consists of:

● State: It is represented by the attributes of an object. It also reflects the


properties of an object.
● Behavior: It is represented by the methods of an object. It also reflects the
response of an object to other objects.
● Identity: It gives a unique name to an object and enables one object to interact
with other objects.
A Simple Car Class
A Simple Car Class
self Parameter

● The self parameter is a reference to the current instance of the class and
is used to access variables that belong to the class.
● It must be the first parameter of any function in the class (methods) so
that they can access the attributes and other methods of the class.
● The name self is a convention and not a keyword. You can use any name,
but self is the widely accepted convention.
__init__ Method

● The __init__ method is a special method in Python classes, known as the


constructor.
● It is automatically called when a new instance of the class is created.
● It initializes the attributes of the class.
● The __init__ method in Python is not mandatory. However, it is
commonly used to initialize an object's state when it is created. If you do
not define an __init__ method, Python provides a default constructor
that does nothing.
A Rectangle Class
When __init__ is Not Used
Inheritance

● Inheritance is a way of creating a new class for using details of


an 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).
Inheritanc
e
Super Keyword

● The super keyword in Python is used to call a method from the parent
class.
● It is commonly used in the context of inheritance, allowing the child class
to access methods and constructors of the parent class without
explicitly naming the parent class.
Multiple Inheritance

When a class is derived from more than one base class it is called multiple Inheritance.
The derived class inherits all the features of the base class.
Multiple Inheritance
Multiple Inheritance
Multiple Inheritance
Multilevel Inheritance

● Multilevel Inheritance in Python is a type of Inheritance in which a class inherits from


a class, which itself inherits from another class.
● It allows a class to inherit properties and methods from multiple parent classes,
forming a hierarchy similar to a family tree. It consists of two main aspects:

Base class: This represents a broad concept.

Derived classes: These inherit from the base class and add specific traits
Multilevel Inheritance
Multilevel Inheritance
Output
Hierarchical Inheritance

● Hierarchical Inheritance is a type of inheritance in which a single base class is


inherited by multiple derived classes.
● In this scenario, each derived class shares common attributes and methods
from the same base class, forming a hierarchy of classes.
Hierarchical
Inheritance
Hierarchical
Inheritance
Public Access Modifier
Protected Access Modifier:

● The members of a class that are declared protected are only accessible to a
class derived from it.
● Data members of a class are declared protected by adding a single underscore
‘_’ symbol before the data member of that class.
Protected Access Modifier:
Private Access Modifier:


Private Access Modifier:
Encapsulation

● Encapsulation is the bundling of data (attributes) and methods (functions) that operate
on the data into a single unit or class.
● It also restricts direct access to some of an object's components, which is a means of
preventing accidental interference and misuse of the data.
Encapsulation


Encapsulation
Encapsulation
Polymorphism
Polymorphism
Polymorphism

You might also like