OOP Notes
OOP Notes
• Class – A user defined prototype for an object that defines a set of attributes (data members and methods)
that characterize any object of the class.
• Create class
class box:
side = 5
• Create object
b1 = box()
print(b1.side)
• __module__ − Module name in which the class is defined. This attribute is "__main__" in interactive mode.
• __bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in the
base class list.
• All classes have a function called __init__(). It is called automatically every time the class is being used to
create a new object.
• The __init__() function is used to assign values to object properties, or other operations that are necessary
to do when the object is being created,
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
• The self parameter is a reference to the current instance of the class, and is used to access variables that
belong to the class.
Object methods
• The self parameter is a reference to the current instance of the class, and is used to access variables that
belongs to the class. It does not have to be named self , you can call it whatever you like, but it has to be
the first parameter of any function in the class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
Data encapsulation
• Using object-oriented programming in Python we can restrict access to methods and variables. This
prevents data from direct modification. This is called as encapsulation.
• The objects attributes if private are not directly visible outside the class definition. This is called data hiding
or data abstraction
e.g. __max
Class Computer:
def __init__(self):
self.__price=800
Inheritance
• Inheritance allows us to define a class that inherits all the methods and properties from another class.
• Parent class is the class being inherited from, also called base class.
• Child class is the class that inherits from another class, also called derived class.
• To create a class that inherits the functionality from another class, send the parent class as a parameter
when creating the child class:
class Student(Person):
pass
• If __init__ function is added to the child class it will no longer inherit the __init__ function of the parent
class
Types of Inheritance
Polymorphism
Polymorphism in object-oriented programming is an ability to use common interface for multiple forms
Method overloading
• To overload a method in python, the method logic is written in such a way that depending upon the
parameters passed, the code executes accordingly
• It is used in a class where the method may need to handle different number of parameters at runtime
return x+y+z
print(add(2,3))
print(add(2,3,4))
Method overriding
• Thus, the concept of method overriding allows to change or override the parent class function in the child
class.
• Any object.attr is searched for up the namespace tree. The lower definitions override the higher ones
Composition Classes
• Inheritance models what is called an is a relationship. This means that when you have a Derived class that
inherits from a Base class, you created a relationship where Derived is a specialized version of Base.
• Composition is a concept that models a has a relationship. It enables creating complex types by
combining objects of other types. This means that a class Composite can contain an object of another class
Component. This relationship means that a Composite has a Component.
• Composition enables you to reuse code by adding objects to other objects, as opposed to inheriting the
interface and implementation of other classes.
Composition v/s Inheritance
• Composition allows composite classes to reuse the implementation of the components it contains. The
composite class doesn’t inherit the component class interface, but it can leverage its implementation.
• The composition relation between two classes is considered loosely coupled. That means that changes to the
component class rarely affect the composite class, and changes to the composite class never affect the
component class.
• This provides better adaptability to change and allows applications to introduce new requirements without
affecting existing code.
• When looking at two competing software designs, one based on inheritance and another based on
composition, the composition solution usually is the most flexible.
• Inheritance is used where a class wants to derive the nature of parent class and then modify or extend the
functionality of it. Inheritance will extend the functionality with extra features allows overriding of methods,
but in the case of Composition, we can only use that class we can not modify or extend the functionality of
it. It will not provide extra features. Thus, when one needs to use the class as it without any modification, the
composition is recommended and when one needs to change the behavior of the method in another class,
then inheritance is recommended.