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

Unit 4 Python

The document provides detailed explanations and examples of various Python programming concepts, including method overriding, static methods, built-in class attributes, threading, constructors, single inheritance, and multiple inheritance. Each section includes example code to illustrate the concepts. The document serves as a comprehensive guide for understanding these fundamental programming principles in Python.

Uploaded by

robrayl02
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)
5 views

Unit 4 Python

The document provides detailed explanations and examples of various Python programming concepts, including method overriding, static methods, built-in class attributes, threading, constructors, single inheritance, and multiple inheritance. Each section includes example code to illustrate the concepts. The document serves as a comprehensive guide for understanding these fundamental programming principles in Python.

Uploaded by

robrayl02
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/ 3

Here are the detailed answers with example programs:

1. Method Overriding with Example

Method overriding occurs when a subclass provides a specific implementation of a method


that is already defined in its superclass. The method in the subclass should have the same
name, return type, and parameters as in the superclass.

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

2. Static Methods with Example

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

# Calling static method without creating an instance


result = MathOperations.add(5, 10)
print("Sum:", result) # Output: Sum: 15

3. Built-in Class Attributes with Example

Python provides built-in attributes for classes. Some common built-in attributes are:

• __dict__: Stores the class attributes.


• __name__: Returns the class name.
• __module__: Returns the module name.
• __bases__: Returns the base classes.
Example:

class Sample:
def __init__(self, name):
self.name = name

# Creating an object
obj = Sample("Python")

# Accessing built-in class attributes


print("Class Dictionary:", Sample.__dict__)
print("Class Name:", Sample.__name__)
print("Module Name:", Sample.__module__)
print("Base Classes:", Sample.__bases__)

4. Define Thread and Implement Using _thread Module

A thread is a lightweight process that allows a program to run multiple tasks concurrently.

Example using _thread Module:

import _thread
import time

# Function to run in a thread


def print_numbers():
for i in range(5):
print("Number:", i)
time.sleep(1)

# Creating a new thread


_thread.start_new_thread(print_numbers, ())

# Allowing main thread to continue execution


time.sleep(6)
print("Main thread execution finished")

5. Constructor & Parameterized Constructor

A constructor in Python is a special method (__init__) used to initialize an object when it


is created. A parameterized constructor accepts arguments to initialize an object with
specific values.

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}")

# Creating objects with different values


p1 = Person("Alice", 25)
p2 = Person("Bob", 30)

p1.display() # Output: Name: Alice, Age: 25


p2.display() # Output: Name: Bob, Age: 30

6. Program to Implement Single Inheritance

Single inheritance means a child class inherits from a single parent class.

Example:

class Animal:
def speak(self):
print("Animals make sounds")

class Dog(Animal): # Inheriting from Animal


def bark(self):
print("Dog barks")

# Creating an object of Dog


d = Dog()
d.speak() # Output: Animals make sounds
d.bark() # Output: Dog barks

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")

class Child(Father, Mother): # Inheriting from both Father and Mother


def child_traits(self):
print("Child has both traits")

# Creating an object of Child


c = Child()
c.father_traits() # Output: Father's traits
c.mother_traits() # Output: Mother's traits
c.child_traits() # Output: Child has both traits

You might also like