Introduction to OOPand Inheritance
Object-Oriented Programming (OOP):
- Focuses on objects that interact with each other.
- Concepts include classes, objects, inheritance, polymorphism,
encapsulation, and abstraction.
Inheritance:
- Inheritance is the mechanism that allows one class (child) to
inherit attributes and methods from another class (parent).
- Benefits of inheritance: Reusability, Extensibility, Improved code
organization.
3.
Why Use Inheritance?
-Reusability: Child classes can reuse code from
the parent class, avoiding duplication.
- Extensibility: Child classes can extend the
functionality of parent classes.
- Simplifies Maintenance: Changes to the parent
class automatically reflect in the child classes.
4.
Types of Inheritance
-Single Inheritance: A child class inherits from one parent class.
Example: class Dog(Animal):
- Multiple Inheritance: A child class inherits from more than one
parent class.
Example: class Dog(Mammal, Animal):
- Multilevel Inheritance: A class inherits from a class, which is itself
derived from another class.
Example: class Puppy(Dog):
5.
Inheritance Syntax
Basic Syntax:
classParentClass:
def __init__(self):
pass
def method_in_parent(self):
pass
class ChildClass(ParentClass): # Inherits from ParentClass
def __init__(self):
super().__init__() # Calls the constructor of ParentClass
pass
def method_in_child(self):
pass
6.
Creating Objects forthe Class
Class Object Instantiation:
When a class is defined, you can create objects (instances) of the class to
access its methods and attributes.
Example:
class Animal:
def __init__(self, name):
self.name = name
# Creating an object of Animal class
animal = Animal("Lion")
print(animal.name) # Output: Lion
7.
Demonstrating Inheritance withExample
Parent Class:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
Child Class:
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
print(f"{self.name} barks")
Creating Objects for the Child Class:
dog = Dog("Buddy", "Golden Retriever")
dog.speak() # Output: Buddy barks
8.
# Parent class
classParent:
def __init__(self):
self.parent_var = "I am a Parent variable"
def parent_method(self):
print("This is a method from Parent class")
# First Child class
class Child1(Parent):
def child1_method(self):
print("This is a method from Child1 class")
# Second Child class
class Child2(Parent):
def child2_method(self):
print("This is a method from Child2 class")
# Creating objects of child classes
obj1 = Child1()
obj2 = Child2()
# Accessing Parent class properties and methods
print(obj1.parent_var) # Accessing Parent's variable
obj1.parent_method() # Calling
9.
class Animal:
def move(self):
print('Animalscan move')
class Bird:
def fly(self):
print('Birds can fly')
class Bat(Animal, Bird):
def sound(self):
print('Bats make high-pitched sounds')
bat = Bat()
bat.move()
bat.fly()
bat.sound()
Method Overriding inInheritance
Definition: The child class can provide its own implementation of a method that is already
defined in the parent class.
Example:
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal):
def speak(self):
print("Dog barks")
animal = Animal()
animal.speak() # Output: Animal makes a sound
dog = Dog()
dog.speak() # Output: Dog barks
12.
Super Function andInheritance
The super() function is used to call methods from the parent class.
This is particularly useful in the child class constructor to call the parent class's __init__()
method.
Example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call the parent class constructor
self.breed = breed
def speak(self):
print(f"{self.name} barks")
dog = Dog("Buddy", "Golden Retriever")
dog.speak() # Output: Buddy barks
13.
Private and ProtectedAttributes in
Inheritance
Private Attributes:
- Private attributes (using __) are not accessible outside the class, even in subclasses.
Example:
class Animal:
def __init__(self, name):
self.__name = name # Private attribute
def get_name(self):
return self.__name
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
dog = Dog("Buddy")
print(dog.get_name()) # Access private attribute via public method
14.
Access Modifiers inInheritance
• Public Attributes: Accessible from anywhere.
• Protected Attributes: Indicated by a single
underscore (_), meant for internal use within a
class or subclass.
• Private Attributes: Indicated by double
underscores (__), not directly accessible
outside the class or subclass.
15.
Inheritance in Action- Example
Parent Class:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
Child Class:
class Dog(Animal):
def speak(self):
print(f"{self.name} barks")
class Cat(Animal):
def speak(self):
print(f"{self.name} meows")
dog = Dog("Buddy")
cat = Cat("Kitty")
dog.speak() # Buddy barks
cat.speak() # Kitty meows
16.
Conclusion
- Inheritance allowsone class to acquire
attributes and methods from another.
- It promotes code reuse and provides a way to
extend or modify functionality.
- You can override methods in the child class and
use super() to call the parent class's methods.