Python Class Inheritence Exercises

Let’s check out some exercises that will help understand Class Inheritences better.

Exercise 1-a: New Class Which Inherits

A new class named F14 is initated for you which inherits from the parent class Jets.

Instead of taking parameters other than self such as name and country, initiate the new class so that name is always fixed to "F14" and origin is always fixed to "USA"

Make sure the new class has its own initation method (constructor or __init__) ehich takes only one parameter:self and overrides name and origin attributes as those don't change for an F14 fighter jet.


Note that new class inherits the parent class by having it inside its parenthesis as following:

class F14(Jets):
Inside the new class you can start defining it with the initiator method __init__
class F14(Jets):
    def __init__(self):
class F14(Jets):

    def __init__(self):
        self.name = "F14"
        self.origin = "USA"

Exercise 1-b: __init__ Constructor and New Attributes

A new class named F14 is initated for you which inherits from the parent class Jets.

Under this new class, define 4 methods regarding engine, seat, tail and speed.

Make sure the new class has its own initation method (constructor or __init__) ehich takes only one parameter:self and overrides name and origin attributes to "F14" and "USA" always as those don't change for an F14 fighter jet.

Also add 3 more attributes:
engine, seat, tail which are all 2 by default.


Again you need to start with inheriting the parent class as following and then start constructing its own attributes.
class F14(Jets):

Second step will be the initiation method:

class F14(Jets):

    def __init__(self):        

After that you can continue defining your attributes inside the method.

Once attributions are defined correctly you’ll need to create an instance of F16 and assign it to the variable a as a final step.

a=F16()
class F14(Jets):
    
    def __init__(self):
        self.name="F14"
        self.origin="USA"
        self.engine=2
        self.seat=2
        self.tail=2

    
a=F14()
print(a.engine)
print(a.seat)
print(a.origin)

Exercise 1-c: Class Inheritence from Scratch

From scratch create a new class named AJS37. Make it inherit from the parent class Jets. This jet should have an origin attribute of Sweden and name attribute of AJS37. The rest of the attributes doesn't need to be overwriten.

Assign an instance of the new class to the variable b.


You can start a new class called AJS37 and inherit the parent class Jets as below:
class AJS37:

You can start a new class called AJS37 and inherit the parent class Jets as below:
Remember to create needed attributes inside the constructor.

class AJS37(Jets):

def __init__(self):

As a final step you’ll need to assign an instance of your new class to the variable b.
b=AJS37()
class AJS37(Jets):
    def __init__(self):
        self.name="AJS37"
        self.origin="Sweden"
b=AJS37()

Need More Exercises?

Check out Holy Python AI+ for amazing Python learning tools.

*Includes 14 more programming languages, inspirational tools and 1-on-1 consulting options.
Umut Sagir
Finance & Data Science Professional,
Founder of HolyPython