Inheritance is an important idea in object-oriented programming (OOP). It lets new classes, called subclasses, take characteristics and actions from other classes, called parent classes. This makes it easier to use the same code again and helps organize classes in a way that reflects real-life relationships. Let’s explore how inheritance works with properties and methods in subclasses.
In OOP, one class can borrow from another class. The class that gives away its properties and methods is known as the parent class (or superclass). The new class that receives these is called the child class (or subclass).
For example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some sound"
In this example, Animal
is the parent class. It has a property called name
and a method called speak()
.
Now, let's create a subclass:
class Dog(Animal):
def speak(self):
return "Bark"
Here, Dog
is a subclass that inherits from Animal
. It gets the name
property and can change the speak()
method to create its own version.
Inherited Properties: The Dog
subclass takes the name
property from Animal
. If you create a Dog
instance:
my_dog = Dog("Buddy")
print(my_dog.name) # Output: Buddy
my_dog
can use the inherited name
.
Overriding Properties: Subclasses can also have properties that have the same name as their parent class. This creates a new property that hides the parent's property:
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
my_dog = Dog("Buddy", "Golden Retriever")
print(my_dog.breed) # Output: Golden Retriever
Inherited Methods: If Dog
does not change the speak()
method, it will use the one from Animal
.
class Cat(Animal):
pass
my_cat = Cat("Whiskers")
print(my_cat.speak()) # Output: Some sound
Overriding Methods: Subclasses can also create their own versions of inherited methods:
class Cat(Animal):
def speak(self):
return "Meow"
my_cat = Cat("Whiskers")
print(my_cat.speak()) # Output: Meow
Inheritance helps subclasses use or change properties and methods from their parent class. This makes the code cleaner and more organized, placing similar function together in an easy-to-understand structure. With inheritance, developers can build a layered system of classes that are flexible and easy to maintain. This is why OOP is a popular approach in software development.
Inheritance is an important idea in object-oriented programming (OOP). It lets new classes, called subclasses, take characteristics and actions from other classes, called parent classes. This makes it easier to use the same code again and helps organize classes in a way that reflects real-life relationships. Let’s explore how inheritance works with properties and methods in subclasses.
In OOP, one class can borrow from another class. The class that gives away its properties and methods is known as the parent class (or superclass). The new class that receives these is called the child class (or subclass).
For example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some sound"
In this example, Animal
is the parent class. It has a property called name
and a method called speak()
.
Now, let's create a subclass:
class Dog(Animal):
def speak(self):
return "Bark"
Here, Dog
is a subclass that inherits from Animal
. It gets the name
property and can change the speak()
method to create its own version.
Inherited Properties: The Dog
subclass takes the name
property from Animal
. If you create a Dog
instance:
my_dog = Dog("Buddy")
print(my_dog.name) # Output: Buddy
my_dog
can use the inherited name
.
Overriding Properties: Subclasses can also have properties that have the same name as their parent class. This creates a new property that hides the parent's property:
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
my_dog = Dog("Buddy", "Golden Retriever")
print(my_dog.breed) # Output: Golden Retriever
Inherited Methods: If Dog
does not change the speak()
method, it will use the one from Animal
.
class Cat(Animal):
pass
my_cat = Cat("Whiskers")
print(my_cat.speak()) # Output: Some sound
Overriding Methods: Subclasses can also create their own versions of inherited methods:
class Cat(Animal):
def speak(self):
return "Meow"
my_cat = Cat("Whiskers")
print(my_cat.speak()) # Output: Meow
Inheritance helps subclasses use or change properties and methods from their parent class. This makes the code cleaner and more organized, placing similar function together in an easy-to-understand structure. With inheritance, developers can build a layered system of classes that are flexible and easy to maintain. This is why OOP is a popular approach in software development.