0xCODE
Published in

0xCODE

Object Inheritance

Using A Class And Instantiating Objects With Inheritance In Python

Creating objects using a class allows the developer to define features or characteristics called attributes, which can be inherited by a child or daughter class. Following the object oriented methodology, a child class can be instantiated as an object that can inherit the attributes of the parent class. This is the process of inheritance, much like how a benefactor can pass on their wealth or assets to a beneficiary. In this case it is passing down attributes to a child object i.e. inheritance class.

The class is an object that also contains the methods and properties for transforming data. The object we specify contains the attributes as variables, which store values that programs can modify. From this object, we can create other objects that we refer to as the child or daughter class. These objects inherit what the parent contains, and also specify their own unique attributes that can distinguish them from another child class. The parent class is the blueprint or template from which other classes can inherit from.

Creating A Class

We create an object and specify its methods that can change the state of the object. The object is the class itself, and it contains attributes that contain values. These values can be changed, but it is still a part of the object and does not change it into another object. It still falls under the class to which it belongs to.

If we take for example a car, there are certain details that is consistent regardless of the type or make. A car has a make, model, year and color as details. In Python, we create the object using the class keyword and start with defining it.

class Car:       <attributes>

Using the def keyword, we initialize a function that contains the details as variables that are the attributes of the object. This requires the __init__ method and the use of the self keyword that represents the instance of the parent class.

class Car:
def __init__(self, carMake, caModel, carYear, carColor):

The variables defined are:

carMakecarModelcarYearcarColor

Next, the variables are declared as an instance of self. Take the self keyword and assign it values using the dot (.) notation to make access to the attribute. Following the dot, use a simple word to describe the instance. In this case, I will just define a make, model, year and color.

self.make
self.model
self.year
self.color

The values can be the same as the variables defined, but it does not have to.

class Car:
def __init__(self, carMake, carModel, carYear, carColor):
self.make = carMake
self.model = carModel
self.year = carYear
self.color = carColor

Inheritance Class

We can now create objects from the parent class Car. These are different types of cars that inherit from the main car object. If you have a Tesla Model S and a Toyota Corolla, they are both cars. One is an electric vehicle (EV) and the other is a conventional gasoline engine type sedan. Likewise, the Ford Everest and Lexus IS350 are also cars. They are not the same type though, since one is an SUV while the latter is a luxury sedan. The difference is in their features, which can be attributes that are unique to each car.

The Car object is more a generalization of a what a car’s details are. Now it is time to get specific about the type of car. The inheritance class must be defined just like a class, but it must take a parameter value of the Car class.

For Tesla Model S, we define an instance of the Car object as:

class teslaEV(Car):
def __init__(self, teslaMotor, teslaBattery, teslaMake, teslaModel, teslaYear, teslaColor)
Car.__init__(self, teslaMake, teslaModel, teslaYear, teslaColor)
self.motor = teslaMotor
self.battery = teslaBattery

The teslaEV class has two attributes that are not found in non-EV cars. A Tesla has a motor (teslaMotor) instead of a gasoline engine and its power source is from a battery (teslaBattery). The variables teslaMotor and teslaBattery indicate the type of motor (dual or tri-motor configuration) and battery capacity (in kWh).

Another example is for creating an object for a Ford Everest SUV.

class fordSUV(Car):
def __init__(self, fordTowCapacity, fordGroundClearance, fordMake, fordModel, fordYear, fordColor)
Car.__init__(self, fordMake, fordModel, fordYear, fordColor)
self.towcapacity = fordTowCapacity
self.groundclearance = fordGroundClearance

The fordSUV class was instantiated from the parent class Car. As an SUV type, it has a rated towing capacity and measured ground clearance.

Class Inheritance Examples

Now that we have defined the parent and child classes, we can use some examples to show they function.

Here is the example code (Python 3.6.x):

class Car:
def __init__(self, carMake, carModel, carYear, carColor):
self.make = carMake
self.model = carModel
self.year = carYear
self.color = carColor
class teslaEV(Car):
def __init__(self, teslaMotor, teslaBattery, teslaMake, teslaModel, teslaYear, teslaColor)
Car.__init__(self, teslaMake, teslaModel, teslaYear, teslaColor)
self.motor = teslaMotor
self.battery = teslaBattery
class fordSUV(Car):
def __init__(self, fordTowCapacity, fordGroundClearance, fordMake, fordModel, fordYear, fordColor)
Car.__init__(self, fordMake, fordModel, fordYear, fordColor)
self.towcapacity = fordTowCapacity
self.groundclearance = fordGroundClearance

Let us create a new car of the Tesla type and call it myTesla.

myTesla = teslaEV("Dual-Motor", "100 kWh", "Tesla", "Model S", "2017", "Red")

Now lets get its attributes by specifying the object data.

print(myTesla.make, myTesla.model, myTesla.battery)

This returns the following result:

Tesla Model S 100 kWh

We can also create another class from the child class that can inherit multiple attributes. Consider the following instance of the teslaEV class:

class teslaSpecialEdition(teslaEV):
def __init__(self, teslaSEDesign, teslaSEMotor, teslaSEBattery, teslaSEMake, teslaSEModel, teslaSEYear, teslaSEColor):
teslaEV.__init__(self, teslaSEMotor, teslaSEBattery, teslaSEMake, teslaSEModel, teslaSEYear, teslaSEColor)
self.design = teslaSEDesign

We have instantiated a class from a child class. We are now inheriting the attributes from the Car and the teslaEV classes. The teslaSpecialEdition class has a design attribute called teslaSEDesign. Let us assign to the design the value of “Space Man”. We can then get that attribute and a few others in the following code:

mySpecialTesla = teslaSpecialEdition("Space Man", "Dual-Motor", "100 kWh", "Tesla", "Model S", "2017", "Red")print(mySpecialTesla.make, mySpecialTesla.model, mySpecialTesla.design)

This returns the output:

Tesla Model S Space Man

Synopsis

Creating objects from classes can support inheritance from a parent to a child class. This allows instantiating different objects from a main class that contains the general attributes of the objects. This allows the creation of object types that can inherit the methods and properties that don’t have to be created again in the instance of an object.

--

--

--

Essays and Articles on software engineering, development and computer science.

Recommended from Medium

Beginner friendly introduction into DevOps with Docker on Windows

a screenshot of docker compose file used by me

News Update Times

How To Automatically Sort Photos Into a Separate Folder on MacOS

Ruby Functions on Cloud Run! 💎

AidStream numbers in 2018

MongoDB Presents an Evening With Eliot Horowitz and Stitch

Why Automate? Writing a self-testing Python class for REST or XML API invocation

Domain Driven Design Part I: Strategic Design and Integration Patterns

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Tabora

Vincent Tabora

Editor HD-PRO, DevOps Trusterras (Cybersecurity, Blockchain, Software Development, Engineering, Photography, Technology)

More from Medium

Brute Force and Exhaustive Search

Full-Powered Binary Search with `bisect` in Python 3.10

Why is Python the best choice for Android app development?

Counter in Python-What is Counter & how to use it.