
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inheritance vs Instantiation for Python Classes
In Python, inheritance is the capability of one class to derive or inherit the properties from another class. The class that derives properties is called the derived class or child class, and the class from which the properties are being derived is called the base class or parent class.
In other words, inheritance refers to defining a new class with little or no modification to an existing class. Following is the syntax of the inheritance -
class A: #class A (base class) pass class B(A): #class B (derived class) pass
In the following example, we have inherited 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 defined in base class
Following is the output of the above code -
Hello welcome to Tutorialspoint Hello welcome to Python Tutorial
Method Overriding
Method overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.
When a method in a subclass has the same name, same parameters or signature, and same return type(or subtype) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
Example
In the following example, method1
is defined in both the base class and the derived class. When method1
is called using an instance of the derived class, the version in the derived class overrides the one in the base class -
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 classs def method1(self): print("Java is a programming language") def method(self): print("Hello, welcome to Java Tutorial") obj1=Java() obj1.method() # calling method defined in the derived class obj1.method1() #calling method defined in the base class(method override )
Following is the output of the above code -
Hello, welcome to Java Tutorial Java is a programming language
Instantiation
Instantiating a class creates a copy of the class, which inherits all class variables and methods. Instantiating a class in Python is simple. To instantiate a class, we simply call the class as if it were a function, passing the arguments that the __init__ method defines. The return value will be the newly created object.
Example
Following is an example of instantiating a class -
class Foo(): def __init__(self,x,y): print(x+y) f = Foo(45,8) #instantiation
Following is the output of the above code -
53