Member-only story
Python OOPS Challenge | Day 8 |What is the output of following Python code?
In this code snippet, we have two classes: Fruit and Apple. The Apple class inherits from the Fruit class.
Code Analysis
1. The Fruit class has an __init__ method (constructor) that prints ‘1'.
2. The Apple class also has its own __init__ method that overrides the one from Fruit and prints ‘2'.
When we create an instance of Apple with obj = Apple(), Python will look for the __init__ method in the Apple class first. Since Apple has its own __init__ method, it overrides the __init__ method of Fruit. Therefore, only ‘2' is printed.
Output
The output of this code will be:
2
So, the correct answer is the second option: 2.