Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain Inheritance vs Instantiation for Python classes.
In Python, inheritance and instantiation are two fundamental object-oriented programming concepts. Inheritance allows one class to derive properties from another class, while instantiation creates objects from a class definition.
Understanding Inheritance
Inheritance is the capability of one class to derive or inherit properties from another class. The class that derives properties is called the derived class or child class, and the class from which properties are being derived is called the base class or parent class.
Syntax
class Parent: # base class
pass
class Child(Parent): # derived class inherits from Parent
pass
Example
In the following example, we inherit method1() in the Derived class from the Base class ?
class Base:
def method1(self):
print("Hello welcome to Python Tutorial")
class Derived(Base):
def method2(self):
print("Hello welcome to Tutorialspoint")
obj = Derived()
obj.method2() # method is defined in derived class
obj.method1() # method is inherited from base class
Hello welcome to Tutorialspoint Hello welcome to Python Tutorial
Method Overriding
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its parent class. When a method in a subclass has the same name and parameters as a method in its parent class, the subclass method overrides the parent method.
Example
In the following example, method1 is defined in both classes. The derived class version overrides the base class version ?
class Python: # base class
def method1(self):
print("Python is a programming language")
def method2(self):
print("Python is an interpreted language")
class Java(Python): # derived class
def method1(self):
print("Java is a programming language")
def method3(self):
print("Hello, welcome to Java Tutorial")
obj1 = Java()
obj1.method3() # calling method defined in the derived class
obj1.method1() # calling overridden method
obj1.method2() # calling inherited method
Hello, welcome to Java Tutorial Java is a programming language Python is an interpreted language
Understanding Instantiation
Instantiation is the process of creating an object (instance) from a class definition. When you instantiate a class, you create a copy of the class that inherits all class variables and methods. To instantiate a class, we call the class as if it were a function, passing arguments to the __init__ method.
Example
class Calculator:
def __init__(self, x, y):
self.x = x
self.y = y
print(f"Calculator created with values {x} and {y}")
def add(self):
return self.x + self.y
# Instantiation - creating objects from the class
calc1 = Calculator(45, 8)
calc2 = Calculator(10, 20)
print(f"Sum from calc1: {calc1.add()}")
print(f"Sum from calc2: {calc2.add()}")
Calculator created with values 45 and 8 Calculator created with values 10 and 20 Sum from calc1: 53 Sum from calc2: 30
Key Differences
| Aspect | Inheritance | Instantiation |
|---|---|---|
| Purpose | Create new classes based on existing classes | Create objects from class definitions |
| Relationship | Class-to-class relationship | Class-to-object relationship |
| Syntax | class Child(Parent): |
obj = ClassName() |
| Result | New class definition | Object instance |
Conclusion
Inheritance allows classes to share and extend functionality through parent-child relationships, while instantiation creates working objects from class blueprints. Both concepts work together to enable code reusability and object-oriented design in Python.
