in Python by
Q:
What Is Inheritance In Python Programming?

1 Answer

0 votes
by
Inheritance is an OOP mechanism which allows an object to access its parent class features. It carries forward the base class functionality to the child.

Python Interview Questions - Inheritance

We do it intentionally to abstract away the similar code in different classes.

The common code rests with the base class, and the child class objects can access it via inheritance. Check out the below example.

class PC: # Base class

    processor = "Xeon" # Common attribute

    def set_processor(self, new_processor):

        processor = new_processor

class Desktop(PC): # Derived class

    os = "Mac OS High Sierra" # Personalized attribute

    ram = "32 GB"

class Laptop(PC): # Derived class

    os = "Windows 10 Pro 64" # Personalized attribute

    ram = "16 GB"

desk = Desktop()

print(desk.processor, desk.os, desk.ram)

lap = Laptop()

print(lap.processor, lap.os, lap.ram)

The output:

Xeon Mac OS High Sierra 32 GB

Xeon Windows 10 Pro 64 16 GB
Click here to read more about Python
Click here to read more about Insurance

Related questions

0 votes
asked Dec 14, 2019 in Python by sharadyadav1986
0 votes
asked Jun 24, 2020 in Python by Robindeniel
0 votes
asked Jun 15, 2020 in C Plus Plus by Robindeniel
+1 vote
asked May 14, 2019 in C Plus Plus by anonymous
0 votes
asked Jan 11 in Python by SakshiSharma
0 votes
asked Dec 31, 2020 in Python by SakshiSharma
0 votes
...