[go: up one dir, main page]

0% found this document useful (0 votes)
18 views14 pages

OOP Unit 2 (Part 1)

The document explains key concepts of Object-Oriented Programming (OOP) in Python, including default arguments, inheritance, polymorphism, and various types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to illustrate how these concepts are implemented in Python. The document serves as a guide for understanding OOP principles using Python programming language.

Uploaded by

ksanjot247
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)
18 views14 pages

OOP Unit 2 (Part 1)

The document explains key concepts of Object-Oriented Programming (OOP) in Python, including default arguments, inheritance, polymorphism, and various types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to illustrate how these concepts are implemented in Python. The document serves as a guide for understanding OOP principles using Python programming language.

Uploaded by

ksanjot247
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/ 14

Understanding

OOP with Python


By Prof.Hitesh Prajapati
Default Arguments
Python allows function arguments to have default values. If the
function is called without the argument, the argument gets its default
value.

Python has a different way of representing syntax and default values


for function arguments. Default values indicate that the function argument
will take that value if no argument value is passed during the function call.
The default value is assigned by using the assignment(=) operator of the
form keywordname=value.
Default Arguments

def student(firstname, lastname ='Mark', standard ='Fifth'):


print(firstname, lastname, 'studies in', standard, 'Standard')

student('John')
student('John', 'Gates', 'Seventh')
student('John', 'Gates')
student('John', 'Seventh')
Inheritance in Python
In Python, inheritance allows a child class (subclass) to inherit the
attributes and methods of a parent class (superclass). This is achieved by
specifying the parent class in the child class definition, using parentheses
(superclass)

class Person: emp = Person("Hitesh")


def __init__(self, name): print(emp.getName(), emp.isEmployee())
self.name = name
def getName(self): emp = Employee("Mahesh")
return self.name print(emp.getName(), emp.isEmployee())
def isEmployee(self):
return False

class Employee(Person):
def isEmployee(self):
return True
Inheriting Attributes
class Person:
def __init__(self, salary):
self.salary = salary

class Employee(Person):
def print(self):
print(self.salary)

class Student(Person):
def print(self):
print(self.salary)

e1=Employee( 10000)
e1.print()
e2 = Student(2000)
e2.print()
Subclassing (Calling constructor of parent
class)
When a method or attribute is not found
in the child class, Python searches for it class Cat:
in the parent class and its ancestors. def __init__(self):
This is known as method overriding, self.age = 2
where the child class can override the self.sound = "meow"
method with the same name as the
parent class. class Dog(Cat):
def __init__(self):
In the example provided from Stack super().__init__()
Overflow, the Dog class inherits from self.sound = "bark"
Cat, overriding the sound attribute:
Polymorphism and Overriding
Polymorphism in Python refers to the ability of
an object or function to take on multiple forms.
This can be achieved through method
overriding, method overloading.

Method Overriding

In Python, method overriding occurs when a


child class provides a different implementation
for a method that is already defined in its parent
class. This allows for a single method name to
be used for different types of objects.
Polymorphism and Overriding
class Animal:
def sound(self):
print("Generic animal sound")

class Dog(Animal):
def sound(self):
print("Woof!")

class Cat(Animal):
def sound(self):
print("Meow!")

animals = [Dog(), Cat()]


for animal in animals:
animal.sound() # Output: Woof! and Meow!
Single Inheritance:

Single Inheritance: A child class inherits from a single parent class. This
is the most basic form of inheritance, where a subclass inherits all the
methods and properties from a single superclass. Example:

class Parent:
def method(self):
pass

class Child(Parent):
pass
Multiple Inheritance
Multiple inheritance in Python allows a class to inherit attributes and methods from more than one
superclass. This is achieved by listing the superclasses in the class definition, separated by commas. The
super() function is used to access methods and properties of the superclasses.

Multiple Inheritance: A child class can inherit from multiple parent classes. This type of inheritance
allows a subclass to inherit features from multiple superclasses. Example:

class A:
def method_a(self):
pass

class B:
def method_b(self):
pass

class Child(A, B):


pass
Multilevel Inheritance
A child class inherits from a parent class, which itself inherits from
another parent class. This creates a hierarchical relationship
between classes. Example:

class Grandparent:
def method_grandparent(self):
pass

class Parent(Grandparent):
pass

class Child(Parent):
pass
Hierarchical Inheritance
Hierarchical Inheritance: In this type of inheritance, a single parent class
has multiple child classes. Each child class inherits features from the parent
class. Example:

class Parent:
def method_parent(self):
pass

class Child1(Parent):
pass

class Child2(Parent):
pass
Hybrid Inheritance
Hybrid Inheritance: This type combines multiple inheritance with multilevel
inheritance, where a child class inherits from multiple parent classes, and
some of those parent classes have their own parent-child relationships.
Example:
The Lookup Tree

You might also like