Knowee
Questions
Features
Study Tools

What is the output of the following code?     class Parent:        def show(self):            print("Parent")    class Child(Parent):        def show(self):            print("Child")    obj = Child()    obj.show()

Question

What is the output of the following code?     class Parent:        def show(self):            print("Parent")    class Child(Parent):        def show(self):            print("Child")    obj = Child()    obj.show()

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The output of the code will be "Child".

Here's the step by step explanation:

  1. A class named Parent is defined with a method show(self) that prints "Parent".

  2. A class named Child is defined which inherits from the Parent class. This class also has a method show(self) that prints "Child". This is an example of method overriding in object-oriented programming. The Child class is overriding the show(self) method of the Parent class.

  3. An object obj of the Child class is created.

  4. The show() method of the obj object is called. Since the Child class has overridden the show(self) method of the Parent class, "Child" is printed.

This problem has been solved

Solution 2

The output of the code will be "Child".

Here's the step by step explanation:

  1. A class named Parent is defined with a method show(self) that prints "Parent".

  2. A class named Child is defined which inherits from the Parent class. It also has a method show(self) that prints "Child". This is an example of method overriding in object-oriented programming. The Child class is overriding the show(self) method of the Parent class.

  3. An object obj of the Child class is created.

  4. The show() method of the obj object is called. Since obj is an instance of Child, the show(self) method of the Child class is executed, which prints "Child".

This problem has been solved

Similar Questions

What is the output of the following code?        python    class A:        def display(self):            print("Class A")    class B(A):        def display(self):            print("Class B")    class C(A):        def display(self):            print("Class C")    obj = B()    obj.display()    obj = C()    obj.display()

what is the output of following python code? class myclass:    def display(self,a):        print(a)o=myclass()o.display("Hello World")

What will be the output of the following Python code?class Demo: def __init__(self): self.a = 1 self.__b = 1  def display(self): return self.__b obj = Demo()print(obj.__b)

class Parent: def __init__(self): self.__num=56 def show(self): print("Parent:",self.__num)class Child(Parent): def __init__(self): super().__init__() self.__var=89 def show(self): print("Child:",self.__var)parent=Parent()parent.show()child=Child()child.show()

What will be the output of the following Python code?class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags)obj=fruits()obj.display()

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.