KEMBAR78
Basic_concepts_of_OOPS_in_Python.pptx
What is a class ?
A class is logical grouping of attributes(variables)
and methods(functions)
Syntax:
class ClassName:
# class body
Example:
class Student:
# class body
What is an object ?
Object is an instance of a class that has access to all the
attributes and methods of that class.
Syntax:
objectName = ClassName()
Example:
student = Student() # object instantiation
# The object student now has access to all the attributes
and methods of the class Student.
What is object instantiation ?
The process of creation of an object of a class is called instantiation
CLASSES AND OBJECTS
classesAndObjects.py
ATTRIBUTES AND METHODS
What is the self parameter ?
Every instance method accepts has a default parameter
that is being accepted. By convention, this parameter is
named self.
The self parameter is used to refer to the attributes of
that instance of the class.
Example:
class Student:
def setName(self, name):
self.name = name
student = Student()
student.setName(‘Santosh’)
# The above statement is inferred as
student.setName(‘Santosh’) where the object employee
is taken as the self argument.
What is an init method ?
An init method is the constructor of a class that can
be used to initialize data members of that class.
It is the first method that is being called on creation
of an object.
Syntax:
def __init__(self):
# Initialize the data members of the class
Example:
class Employee:
def __init__(self):
print("Welcome!")
employee = Employee()
# This would print Welcome! since __init__ method
was called on object instantiation
What is a class attribute ?
An attribute that is common across all instances of a class is called a class attribute.
Class attributes are accessed by using class name as the prefix.
Syntax:
class className:
classAttribute = value
Example:
class Student:
# This attribute is common across all instances of this class
numberOfStudents = 0
studentOne = Student ()
Student.numberOf Students += 1
print(studentOne.numberOfStudents)
What are data members ?
Data members are attributes declared within a class. They are properties that further define a
class.
There are two types of attributes:
1. Class attributes
2. Instance attributes.
What is an instance attribute ?
An attribute that is specific to each instance of a class is called an instance attribute.
Instance attributes are accessed within an instance method by making use of the self object.
Syntax:
class className:
def methodName(self):
self.instanceAttribute = value
Example:
class Student:
def studentDetails(self, name): # name is the instance attribute
self.name = name
What is an instance method ?
A method which can access the instance attributes of a class by making use of self object is
called an instance method.
Syntax:
def methodName(self):
# method body
Example:
class Employee:
# employeeDetails() is the instance method
def employeeDetails(self, name):
self.name = name
What are methods ?
A function within a class that performs a specific action is called a method belonging to that
class.
Methods are of two types:
1. Static method
2. Instance method
What is a static method ?
A method that does not have access to any of the instance attributes of a class is called a static method.
Static method uses a decorator @staticmethod to indicate this method will not be taking the default self
parameter.
Syntax:
@staticmethod
def methodName(): # Observe that self is not being declared since this is a static method
# method body
Example:
class Student :
numberOfStudents = 0
@StaticMethod # static method that updates the class attribute of the class Student.
def updateNumberOfStudents():
Student.numberOf Students += 1
studentOne = Student()
studentTwo = Student()
studentOne.updateNumberOfStudents()
studentTwo.updateNumberOfStudents()
print(Student.numberOfStudents)
What are the ways to access the attributes and methods of a class ?
Attributes and methods of a class can be accessed by creating an object and accessing the attributes
and objects using class name or object name depending upon the type of attribute followed by the dot
operator (.) and the attribute name or method name.
Example:
class Employee:
numberOfStudents = 0
def printNumberOfStudents (self):
print(Student.numberOfStudents)
student = Student()
Student.numberOf Students += 1 # Modify class attribute using dot operator
student.printNumberOfStudents() # Call the instance method using dot operator
attributesAndMethods.py
ABSTRACTION AND ENCAPSULATION
What is encapsulation ?
Hiding the implementation details from the end user is called as
encapsulation.
Example:
Inner Implementation detail of a Mobile phone, how keypad
button and display screen are connect with each other using circuits.
What is abstraction ?
Abstraction is the process of steps followed to achieve
encapsulation.
Example:
Outer Look of a Mobile Phone, like it has a display screen and
Keypad buttons to dial a number.
abstractionAndEncapsulation.py
INHERITANCE
What is inheritance ?
Inheriting the attributes and methods of a base class into a derived class is called Inheritance.
Syntax:
class BaseClass:
# Body of BaseClass
class DerivedClass(BaseClass):
# Body of DerivedClass
Example:
class Shape:
unitOfMeasurement = 'centimetre’
class Square(Shape):
def __init__(self):
print("Unit of measurement for this square: ", self.unitOfMeasurement)
# The attribute unitOfMeasurement has been inherited from the class Shape to this class
Square
square = Square()
Parent Class
Derived Class
What is multiple inheritance ?
Mechanism in which a derived class inherits from two or more base classes is called a multiple inheritance.
Syntax:
class baseClassOne:
# Body of baseClassOne
class baseClassTwo:
# Body of baseClassTwo
class derivedClass(baseClassOne, baseClassTwo):
# Body of derived class
Example:
class OperatingSystem:
multiTasking = True
class Apple:
website = 'www.apple.com’
class MacOS(OperatingSystem, Apple):
def __init__(self):
if self.multiTasking is True:
print(“{}”.format(self.website))
mac = MacOS()
# class MacOS has inherited 'multitasking' attribute
from the class OperatingSystem and 'website'
attribute from the class 'Apple'
Parent Class
1
Parent Class
2
Derived Class
What is multilevel inheritance ?
Mechanism in which a derived class inherits from a base class which has been derived from another base
class is called a multilevel inheritance.
Syntax:
class BaseClass:
# Body of baseClass class
DerivedClassOne(BaseClass):
# Body of DerivedClassOne class
DerivedClassTwo(DerivedClassOne):
# Body of DerivedClassTwo
Example:
class Apple:
website = 'www.apple.com’
class MacBook(Apple):
deviceType = 'notebook computer’
class MacBookPro(MacBook):
def __init__(self):
print("This is a {}. Visit {} for more
details".format(self.deviceType,
self.website))
macBook = MacBookPro()
# MacBookPro class inherits deviceType from the base class
MacBook. It also inherits website from base class of MacBook,
which is Apple
Parent Class
Child Class
Grand Child Class
What are the naming conventions used for private, protected and public members ?
Private -> __memberName
Protected -> _memberName
Public -> memberName
How is name mangling done for private members by Python ?
Name mangling is done by prepending the member name with an underscore
and class name.
_className__memberName
What is an abstract base class ?
A base class which contains abstract methods that are to be overridden in its derived class is called an
abstract base class.
They belong to the abc module.
Example:
from abc import ABCMeta, abstractmethod
class Shape(metaclass = ABCMeta):
@abstractmethod
def area(self):
return 0
class Square(Shape):
def area(self, side)
return side * side
inheritance.py
POLYMORPHISM
What is polymorphism ?
The same interface existing in different forms is called polymorphism.
Example :
An addition between two integers 2 + 2 return 4 whereas an addition between two
strings "Hello" + "World" concatenates it to "Hello World"
Why is super() method used ?
super() is used to access the methods of base class.
Example:
class BaseClass:
def baseClassMethod():
print("This is BaseClassOne")
class DerivedClass(BaseClass):
def __init__(self):
super().baseClassMethod() # calls the base class method
What is operator overloading ?
Redefining how an operator operates its operands is called operator overloading.
Syntax:
def __operatorFunction__(operandOne, operandTwo):
# Define the operation that has to be performed
Example:
class Square:
def __init__(self, side):
self.side = side
def __add__(sideOne, sideTwo):
return(sideOne.side + sideTwo.side)
squareOne = Square(10)
squareTwo = Square(20)
print("Sum of sides of two squares = ", squareOne + squareTwo)
# After overloading __add__ method, squareOne + squareTwo is interpreted as
Square.__add__(squareOne, squareTwo)
What is overriding ?
Modifying the inherited behavior of methods of a base class in a derived class is called overriding.
Syntax:
class BaseClass:
def methodOne(self):
# Body of method
class DerivedClass(baseClass):
def methodOne(self):
# Redefine the body of methodOne
Example:
class Shape:
def area():
return 0
class Square(Shape):
def area(self, side):
return (side * side)
polymorphism.py
Mini Project
mini_banking_project.py

Basic_concepts_of_OOPS_in_Python.pptx

  • 2.
    What is aclass ? A class is logical grouping of attributes(variables) and methods(functions) Syntax: class ClassName: # class body Example: class Student: # class body What is an object ? Object is an instance of a class that has access to all the attributes and methods of that class. Syntax: objectName = ClassName() Example: student = Student() # object instantiation # The object student now has access to all the attributes and methods of the class Student. What is object instantiation ? The process of creation of an object of a class is called instantiation CLASSES AND OBJECTS
  • 3.
  • 4.
    ATTRIBUTES AND METHODS Whatis the self parameter ? Every instance method accepts has a default parameter that is being accepted. By convention, this parameter is named self. The self parameter is used to refer to the attributes of that instance of the class. Example: class Student: def setName(self, name): self.name = name student = Student() student.setName(‘Santosh’) # The above statement is inferred as student.setName(‘Santosh’) where the object employee is taken as the self argument. What is an init method ? An init method is the constructor of a class that can be used to initialize data members of that class. It is the first method that is being called on creation of an object. Syntax: def __init__(self): # Initialize the data members of the class Example: class Employee: def __init__(self): print("Welcome!") employee = Employee() # This would print Welcome! since __init__ method was called on object instantiation
  • 5.
    What is aclass attribute ? An attribute that is common across all instances of a class is called a class attribute. Class attributes are accessed by using class name as the prefix. Syntax: class className: classAttribute = value Example: class Student: # This attribute is common across all instances of this class numberOfStudents = 0 studentOne = Student () Student.numberOf Students += 1 print(studentOne.numberOfStudents) What are data members ? Data members are attributes declared within a class. They are properties that further define a class. There are two types of attributes: 1. Class attributes 2. Instance attributes.
  • 6.
    What is aninstance attribute ? An attribute that is specific to each instance of a class is called an instance attribute. Instance attributes are accessed within an instance method by making use of the self object. Syntax: class className: def methodName(self): self.instanceAttribute = value Example: class Student: def studentDetails(self, name): # name is the instance attribute self.name = name
  • 7.
    What is aninstance method ? A method which can access the instance attributes of a class by making use of self object is called an instance method. Syntax: def methodName(self): # method body Example: class Employee: # employeeDetails() is the instance method def employeeDetails(self, name): self.name = name What are methods ? A function within a class that performs a specific action is called a method belonging to that class. Methods are of two types: 1. Static method 2. Instance method
  • 8.
    What is astatic method ? A method that does not have access to any of the instance attributes of a class is called a static method. Static method uses a decorator @staticmethod to indicate this method will not be taking the default self parameter. Syntax: @staticmethod def methodName(): # Observe that self is not being declared since this is a static method # method body Example: class Student : numberOfStudents = 0 @StaticMethod # static method that updates the class attribute of the class Student. def updateNumberOfStudents(): Student.numberOf Students += 1 studentOne = Student() studentTwo = Student() studentOne.updateNumberOfStudents() studentTwo.updateNumberOfStudents() print(Student.numberOfStudents)
  • 9.
    What are theways to access the attributes and methods of a class ? Attributes and methods of a class can be accessed by creating an object and accessing the attributes and objects using class name or object name depending upon the type of attribute followed by the dot operator (.) and the attribute name or method name. Example: class Employee: numberOfStudents = 0 def printNumberOfStudents (self): print(Student.numberOfStudents) student = Student() Student.numberOf Students += 1 # Modify class attribute using dot operator student.printNumberOfStudents() # Call the instance method using dot operator
  • 10.
  • 11.
    ABSTRACTION AND ENCAPSULATION Whatis encapsulation ? Hiding the implementation details from the end user is called as encapsulation. Example: Inner Implementation detail of a Mobile phone, how keypad button and display screen are connect with each other using circuits. What is abstraction ? Abstraction is the process of steps followed to achieve encapsulation. Example: Outer Look of a Mobile Phone, like it has a display screen and Keypad buttons to dial a number.
  • 12.
  • 13.
    INHERITANCE What is inheritance? Inheriting the attributes and methods of a base class into a derived class is called Inheritance. Syntax: class BaseClass: # Body of BaseClass class DerivedClass(BaseClass): # Body of DerivedClass Example: class Shape: unitOfMeasurement = 'centimetre’ class Square(Shape): def __init__(self): print("Unit of measurement for this square: ", self.unitOfMeasurement) # The attribute unitOfMeasurement has been inherited from the class Shape to this class Square square = Square() Parent Class Derived Class
  • 14.
    What is multipleinheritance ? Mechanism in which a derived class inherits from two or more base classes is called a multiple inheritance. Syntax: class baseClassOne: # Body of baseClassOne class baseClassTwo: # Body of baseClassTwo class derivedClass(baseClassOne, baseClassTwo): # Body of derived class Example: class OperatingSystem: multiTasking = True class Apple: website = 'www.apple.com’ class MacOS(OperatingSystem, Apple): def __init__(self): if self.multiTasking is True: print(“{}”.format(self.website)) mac = MacOS() # class MacOS has inherited 'multitasking' attribute from the class OperatingSystem and 'website' attribute from the class 'Apple' Parent Class 1 Parent Class 2 Derived Class
  • 15.
    What is multilevelinheritance ? Mechanism in which a derived class inherits from a base class which has been derived from another base class is called a multilevel inheritance. Syntax: class BaseClass: # Body of baseClass class DerivedClassOne(BaseClass): # Body of DerivedClassOne class DerivedClassTwo(DerivedClassOne): # Body of DerivedClassTwo Example: class Apple: website = 'www.apple.com’ class MacBook(Apple): deviceType = 'notebook computer’ class MacBookPro(MacBook): def __init__(self): print("This is a {}. Visit {} for more details".format(self.deviceType, self.website)) macBook = MacBookPro() # MacBookPro class inherits deviceType from the base class MacBook. It also inherits website from base class of MacBook, which is Apple Parent Class Child Class Grand Child Class
  • 16.
    What are thenaming conventions used for private, protected and public members ? Private -> __memberName Protected -> _memberName Public -> memberName How is name mangling done for private members by Python ? Name mangling is done by prepending the member name with an underscore and class name. _className__memberName
  • 17.
    What is anabstract base class ? A base class which contains abstract methods that are to be overridden in its derived class is called an abstract base class. They belong to the abc module. Example: from abc import ABCMeta, abstractmethod class Shape(metaclass = ABCMeta): @abstractmethod def area(self): return 0 class Square(Shape): def area(self, side) return side * side
  • 18.
  • 19.
    POLYMORPHISM What is polymorphism? The same interface existing in different forms is called polymorphism. Example : An addition between two integers 2 + 2 return 4 whereas an addition between two strings "Hello" + "World" concatenates it to "Hello World" Why is super() method used ? super() is used to access the methods of base class. Example: class BaseClass: def baseClassMethod(): print("This is BaseClassOne") class DerivedClass(BaseClass): def __init__(self): super().baseClassMethod() # calls the base class method
  • 20.
    What is operatoroverloading ? Redefining how an operator operates its operands is called operator overloading. Syntax: def __operatorFunction__(operandOne, operandTwo): # Define the operation that has to be performed Example: class Square: def __init__(self, side): self.side = side def __add__(sideOne, sideTwo): return(sideOne.side + sideTwo.side) squareOne = Square(10) squareTwo = Square(20) print("Sum of sides of two squares = ", squareOne + squareTwo) # After overloading __add__ method, squareOne + squareTwo is interpreted as Square.__add__(squareOne, squareTwo)
  • 21.
    What is overriding? Modifying the inherited behavior of methods of a base class in a derived class is called overriding. Syntax: class BaseClass: def methodOne(self): # Body of method class DerivedClass(baseClass): def methodOne(self): # Redefine the body of methodOne Example: class Shape: def area(): return 0 class Square(Shape): def area(self, side): return (side * side)
  • 22.
  • 23.
  • 25.