[go: up one dir, main page]

0% found this document useful (0 votes)
4 views3 pages

Oop Lab

Inheritance in Python allows a child class to inherit attributes and methods from a parent class, promoting code reuse and organization. Child classes can override parent methods or use them as is, depending on their needs. This concept helps in reducing code duplication and makes it easier to manage and scale projects.
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)
4 views3 pages

Oop Lab

Inheritance in Python allows a child class to inherit attributes and methods from a parent class, promoting code reuse and organization. Child classes can override parent methods or use them as is, depending on their needs. This concept helps in reducing code duplication and makes it easier to manage and scale projects.
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

Inheritance in Python

Inheritance is a fundamental concept in object-oriented programming that allows one


class (the child or derived class) to inherit attributes (properties) and methods
(functions) from another class (the parent or base class). The main idea behind
inheritance is to promote code reuse and make the code more organized and
manageable.
Here are the key concepts:

1. Parent (Base) Class


A parent class is the class whose properties and methods are inherited by another
class. Think of it as a blueprint that other classes can borrow from.
Example:
python
Copy code
class Animal:
def __init__(self, name):
self.name = name

def sound(self):
print(f"{self.name} makes a sound.")
• Animal is the parent class.
• It has an attribute (name) and a method (sound()).

2. Child (Derived) Class


A child class is the class that inherits from the parent class. It can use the properties
and methods of the parent class and can also have its own unique properties and
methods.
Example:
python
Copy code
class Dog(Animal):
def sound(self):
print(f"{self.name} barks.")
• Dog is the child class.
• It inherits the name attribute from Animal but overrides the sound() method to
make it more specific to dogs.

3. Code Reusability
By inheriting from a parent class, we can reuse the existing code. For example, instead
of writing a new class from scratch for Dog, we reused the Animal class and only added
the parts specific to Dog.

4. Method Overriding
A child class can override methods from the parent class. This means the child class
can provide its own version of a method that’s already defined in the parent class.
In the Dog class, we override the sound() method:
python
Copy code
class Dog(Animal):
def sound(self):
print(f"{self.name} barks.") # New behavior for Dog class

5. Why Use Inheritance?


• Code organization: You can organize code by grouping shared attributes and
methods in a parent class.
• Avoid duplication: Reuse code instead of writing the same methods again and
again.
• Scalability: It's easier to scale and maintain larger projects when similar
functionality is grouped.

6. Full Example
python
Copy code
class Animal:
def __init__(self, name):
self.name = name

def sound(self):
print(f"{self.name} makes a sound.")

class Dog(Animal):
def sound(self):
print(f"{self.name} barks.")

class Cat(Animal):
def sound(self):
print(f"{self.name} meows.")

# Creating instances
dog = Dog("Buddy")
cat = Cat("Whiskers")

dog.sound() # Output: Buddy barks.


cat.sound() # Output: Whiskers meows.
Summary:
• Inheritance allows one class to inherit properties and methods from another
class.
• It promotes code reusability and reduces duplication.
• Child classes can have their own methods or override parent methods.
• This makes code easier to manage and scale.
method overriding is not compulsory in inheritance. You can absolutely use the
methods from the parent class without overriding them in the child class. In fact, this
is one of the primary benefits of inheritance—reusing the existing methods and
attributes from the parent class in the child class.
If the functionality in the parent class works perfectly for the child class, there is no
need to override the method. The child class can directly inherit and use the method
from the parent class.
Example Without Method Overriding
Let’s look at an example where we don’t override the parent class method:
python
Copy code
class Animal:
def __init__(self, name):
self.name = name

def sound(self):
print(f"{self.name} makes a sound.")

# Child class that inherits from Animal, but does NOT override the sound() method
class Dog(Animal):
pass

# Creating an instance of the Dog class


dog = Dog("Buddy")

# Calling the sound() method, which is inherited from Animal


dog.sound() # Output: Buddy makes a sound.
What's Happening Here:
• Dog is the child class, and it inherits from the Animal class.
• The Dog class does not override the sound() method.
• When we call dog.sound(), it uses the parent class's sound() method.
This shows that you can fully rely on the parent class's methods without needing to
define new ones in the child class unless you want to change or add behavior specific to
the child class.
When to Override:
• Method overriding is only needed if the child class needs to modify or extend
the behavior of the parent class's method. If the parent class's methods already
do what you need, there's no need to override them.
In summary, you can use the parent class's methods as they are, and method overriding
is optional in inheritance.

You might also like