Unit 4 Python
Unit 4 Python
Example:
class Parent:
def show(self):
print("This is the Parent class")
class Child(Parent):
def show(self): # Overriding the parent class method
print("This is the Child class")
# Creating objects
p = Parent()
p.show() # Output: This is the Parent class
c = Child()
c.show() # Output: This is the Child class
A static method in Python is a method that does not receive an implicit reference to an
instance or class. It is defined using the @staticmethod decorator.
Example:
class MathOperations:
@staticmethod
def add(a, b):
return a + b
Python provides built-in attributes for classes. Some common built-in attributes are:
class Sample:
def __init__(self, name):
self.name = name
# Creating an object
obj = Sample("Python")
A thread is a lightweight process that allows a program to run multiple tasks concurrently.
import _thread
import time
Example:
class Person:
def __init__(self, name, age): # Parameterized constructor
self.name = name
self.age = age
def display(self):
print(f"Name: {self.name}, Age: {self.age}")
Single inheritance means a child class inherits from a single parent class.
Example:
class Animal:
def speak(self):
print("Animals make sounds")
7. Multiple Inheritance
Multiple inheritance allows a class to inherit from more than one parent class.
Example:
class Father:
def father_traits(self):
print("Father's traits")
class Mother:
def mother_traits(self):
print("Mother's traits")