Inheritance is a powerful feature of object-oriented programming (OOP) that allows a new class to be based on an existing class, inheriting all its attributes and behaviors. In Python, inheritance is implemented through the use of the keyword “class” and the syntax “class DerivedClass(BaseClass):”.
The BaseClass is also known as the parent class or superclass, while the DerivedClass is also known as the child class or subclass.
Here is an example of how inheritance works in Python:
class Animal: def __init__(self, name): self.name = name def make_sound(self): pass class Dog(Animal): def make_sound(self): print("Woof!") class Cat(Animal): def make_sound(self): print("Meow!")
Inheritance is a powerful feature of object-oriented programming (OOP) that allows a new class to be based on an existing class, inheriting all its attributes and behaviors. In Python, inheritance is implemented through the use of the keyword “class” and the syntax “class DerivedClass(BaseClass):”.
The BaseClass is also known as the parent class or superclass, while the DerivedClass is also known as the child class or subclass.
Here is an example of how inheritance works in Python:
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
print("Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
In this example, we define a base class called Animal that has an init() method and a make_sound() method that doesn’t do anything. We then define two subclasses, Dog and Cat, that inherit from the Animal class.
Both subclasses override the make_sound() method to print out a specific sound that each animal makes. When we create an instance of Dog or Cat, it will have both the attributes and behaviors of the Animal class, as well as its own unique behaviors defined in its own class.
dog = Dog("Buddy") cat = Cat("Whiskers") print(dog.name) # Output: Buddy dog.make_sound() # Output: Woof! print(cat.name) # Output: Whiskers cat.make_sound() # Output: Meow!
In this example, we create instances of the Dog and Cat classes and call their respective make_sound() methods. We can see that each instance has its own name attribute, inherited from the Animal class, and its own unique behavior defined in its own class.
Python Multi-Level inheritance:
Multi-level inheritance in Python is a type of inheritance where a derived class inherits from a parent class, which in turn inherits from another parent class. This creates a chain of inheritance relationships that can be useful in creating complex class hierarchies.
Here is an example of how multi-level inheritance works in Python:
class Animal: def __init__(self, name): self.name = name def make_sound(self): pass class Mammal(Animal): def __init__(self, name): super().__init__(name) def feed_milk(self): print("Feeding milk...") class Dog(Mammal): def make_sound(self): print("Woof!") class Cat(Mammal): def make_sound(self): print("Meow!")
In this example, we have three classes: Animal, Mammal, and two subclasses Dog and Cat that inherit from Mammal. Mammal, in turn, inherits from the Animal class.
The Animal class has an init() method and a make_sound() method that doesn’t do anything. The Mammal class has an init() method that calls the parent class’s init() method using the super() function, and a feed_milk() method.
The Dog and Cat subclasses inherit from Mammal, which means they also inherit from Animal indirectly. Both subclasses override the make_sound() method to print out a specific sound that each animal makes.
dog = Dog("Buddy") cat = Cat("Whiskers") print(dog.name) # Output: Buddy dog.feed_milk() # Output: Feeding milk... dog.make_sound() # Output: Woof! print(cat.name) # Output: Whiskers cat.feed_milk() # Output: Feeding milk... cat.make_sound() # Output: Meow!
In this example, we create instances of the Dog and Cat classes and call their respective methods. We can see that each instance has its own name attribute, inherited from the Animal class, and its own unique behavior defined in its own class. Additionally, the Mammal class has its own behavior, feed_milk(), that is inherited by the Dog and Cat classes through the chain of inheritance.
Python Multiple inheritance:
Multiple inheritance is a feature in Python that allows a subclass to inherit from more than one base class. This means that the subclass can access and use the attributes and methods of all the base classes.
To create a subclass that inherits from multiple base classes, we can specify the base classes in the parentheses after the subclass name, separated by commas. Here is an example:
class Base1: def method1(self): print("Base1 method1") class Base2: def method2(self): print("Base2 method2") class Subclass(Base1, Base2): pass # create an object of the subclass obj = Subclass() # access the methods of the base classes obj.method1() # Output: Base1 method1 obj.method2() # Output: Base2 method2
In the example above, we define two base classes Base1
and Base2
, each with a single method. We then define a subclass Subclass
that inherits from both Base1
and Base2
. Finally, we create an object of the subclass and call the methods of the base classes using the object.
When a subclass inherits from multiple base classes, it may encounter some issues like the Diamond Problem, where multiple base classes have the same method or attribute. However, Python provides a method resolution order (MRO) to determine which method to call when there are conflicts. The MRO is determined at runtime and can be accessed using the __mro__
attribute of a class.
The issubclass(sub,sup) method:
The issubclass(sub, sup)
method is a built-in function in Python that is used to check if a class sub
is a subclass of another class sup
. It returns True
if sub
is indeed a subclass of sup
, and False
otherwise.
The syntax of the issubclass()
method is as follows:
issubclass(sub, sup)
where sub
is the subclass that we want to check, and sup
is the superclass that we want to check against.
Here is an example that demonstrates the use of the issubclass()
method:
class Animal: pass class Dog(Animal): pass class LabradorRetriever(Dog): pass print(issubclass(Dog, Animal)) # Output: True print(issubclass(LabradorRetriever, Dog)) # Output: True print(issubclass(LabradorRetriever, Animal)) # Output: True print(issubclass(Animal, Dog)) # Output: False
In the example above, we define three classes Animal
, Dog
, and LabradorRetriever
. Dog
is a subclass of Animal
, and LabradorRetriever
is a subclass of Dog
.
We then use the issubclass()
method to check if Dog
is a subclass of Animal
, which returns True
. We also check if LabradorRetriever
is a subclass of Dog
and Animal
, which both return True
. Finally, we check if Animal
is a subclass of Dog
, which returns False
.
Note that the issubclass()
method can also be used to check if a class is a subclass of a tuple of classes. For example, issubclass(sub, (sup1, sup2, ...))
checks if sub
is a subclass of any of the classes in the tuple (sup1, sup2, ...)
.
The isinstance (obj, class) method:
The isinstance(obj, class)
method is a built-in function in Python that is used to check if an object obj
is an instance of a class class
. It returns True
if obj
is indeed an instance of class
, and False
otherwise.
The syntax of the isinstance()
method is as follows:
isinstance(obj, class)
where obj
is the object that we want to check, and class
is the class that we want to check against.
Here is an example that demonstrates the use of the isinstance()
method:
class Animal: pass class Dog(Animal): pass class LabradorRetriever(Dog): pass dog = Dog() labrador = LabradorRetriever() print(isinstance(dog, Animal)) # Output: True print(isinstance(labrador, Dog)) # Output: True print(isinstance(labrador, Animal)) # Output: True print(isinstance(dog, LabradorRetriever)) # Output: False
In the example above, we define three classes Animal
, Dog
, and LabradorRetriever
. Dog
is a subclass of Animal
, and LabradorRetriever
is a subclass of Dog
.
We then create two objects dog
and labrador
of the classes Dog
and LabradorRetriever
, respectively.
We then use the isinstance()
method to check if dog
is an instance of Animal
, which returns True
. We also check if labrador
is an instance of Dog
and Animal
, which both return True
. Finally, we check if dog
is an instance of LabradorRetriever
, which returns False
.
Note that the isinstance()
method can also be used to check if an object is an instance of a tuple of classes. For example, isinstance(obj, (class1, class2, ...))
checks if obj
is an instance of any of the classes in the tuple (class1, class2, ...)
.
Method Overriding:
Method overriding is a feature of object-oriented programming (OOP) that allows a subclass to provide a different implementation of a method that is already defined in its superclass. When a method is overridden, the implementation in the subclass takes precedence over the implementation in the superclass.
To override a method in a subclass, we define a method with the same name and signature (i.e., the same number and type of parameters) as the method in the superclass. The method in the subclass must have the super()
function called to invoke the method from the superclass. This allows the subclass method to extend or modify the behavior of the superclass method, while still retaining the original functionality.
Here is an example that demonstrates the concept of method overriding in Python:
class Animal: def speak(self): print("Animal is speaking...") class Dog(Animal): def speak(self): print("Dog is barking...") animal = Animal() dog = Dog() animal.speak() # Output: Animal is speaking... dog.speak() # Output: Dog is barking...
In the example above, we define two classes Animal
and Dog
, where Dog
is a subclass of Animal
. The Animal
class defines a method speak()
, which prints the message “Animal is speaking…”. The Dog
class overrides the speak()
method, and defines its own implementation that prints the message “Dog is barking…”.
We then create two objects animal
and dog
, of the classes Animal
and Dog
, respectively. When we call the speak()
method on each of these objects, we can see that the implementation in the subclass Dog
is used instead of the implementation in the superclass Animal
. This demonstrates the concept of method overriding in Python.
Note that the super()
function is used in the Dog
class to call the speak()
method from the superclass Animal
. This allows the Dog
class to override the method while still retaining the original functionality from the superclass.
Data abstraction in python:
Data abstraction is a key feature of object-oriented programming that allows us to hide the complexity of the implementation of an object and only expose its essential features to the user. In Python, we can achieve data abstraction through the use of abstract classes and interfaces.
An abstract class is a class that cannot be instantiated and is meant to serve as a blueprint for other classes. It contains one or more abstract methods, which are methods that have no implementation in the abstract class but are meant to be implemented in its subclasses. Abstract classes are defined using the abc
module in Python.
Here is an example that demonstrates the use of abstract classes in Python:
import abc class Animal(metaclass=abc.ABCMeta): @abc.abstractmethod def speak(self): pass class Dog(Animal): def speak(self): print("Dog is barking...") dog = Dog() dog.speak() # Output: Dog is barking...
In the example above, we define an abstract class Animal
using the abc
module. The Animal
class contains one abstract method speak()
, which has no implementation in the abstract class but is meant to be implemented in its subclasses.
We then define a subclass Dog
that inherits from the Animal
class and overrides the speak()
method with its own implementation that prints the message “Dog is barking…”.
We then create an object dog
of the Dog
class and call the speak()
method on it. Since Dog
is a subclass of Animal
, it is required to implement the abstract method speak()
, and it does so with its own implementation.
Interfaces are another way to achieve data abstraction in Python. An interface is a collection of abstract methods that define a set of behaviors that an object must implement. In Python, interfaces are not defined as separate constructs like in other programming languages, but rather are achieved by defining abstract classes with only abstract methods.