Python Inheritance and opps

Inheritance is a way of creating a new class from an existing class.

It is a feature of object-oriented programming which is used to define a new class with little or no modification to an existing class. The new class is called the derived class or child class, and the class from which this derived class has been inherited is the base class or parent class. The derived class is formed from a base class, plus it may include some extra additional features. This inheritance concept helps to reuse the code.


class Emoloyee:	#Base Class
class Programmer(Employee):	#Derived or child class

We can use the methods and attributes of Employee in Programmer object.

Also, we can overwrite or add new attributes and methods in the Programmer class.

Type of Inheritance
  1. Single inheritance
  2. Multiple inheritance
  3. Multilevel inheritance

Single Inheritance

Single inheritance occurs when a child class inherits only a single parent class.

Base -> Derived

Multiple Inheritance

Multiple inheritances occurs when the child class inherits from more than one parent class.

Multilevel Inheritance

When a child class becomes a parent for another child class.

Super() method

Super method is used to access the methods of a superclass in the derived class.

super().__init__()  #Calls constructor of the base class

Class methods

A class method is a method which is bound to the class and not the object of the class.

@classmethod decorator is used to create a class method.

Syntax to create a class method:

def (cls, p1, p2):

@property decorators

Consider the following class

class Employee:
	def name(self):
		return self.ename

if e = Employee() is an object of class employee, we can print ( top print the ename/call name() function.

@.getters and @.setters

The method name with @property decorator is called getter method.

We can define a function + @name.setter decorator like below:

def name(self, value):
	self.ename = value

Operator overloading in Python

Operators in python can be overloaded using dunder methods.

These methods are called when a given operator is used on the objects.

Operators in python can be overloaded using the following methods:

p1 + p2 -> p1.__add__(p2)

p1 – p2 -> p1.__sub__(p2)

p1 * p2 -> p1.__mul__(p2)

p1 / p2 -> p1.__truediv__(p2)

p1 // p2 -> p1.__floordiv__(p2)

Other dunder/magic methods in Python

__str__() -> used  to set what gets displayed upon calling str(obj)

__len__() -> used to set what gets displayed upon calling .__len__() or len(obj)

Exercise on Inheritance and Opps

  1. Create a class C-2d vector and use it to create another class representing a 3-d vector.
  2. Create a class of pets from a class Animals and further create class Dog from Pets. Add a method bark to class Dog.
  3. Create a class Employee and add salary and increment properties to it.

Write a method SalaryAfterIncrement method with a @property decorator with a setter which changes the value of increment based on the salary.

  1. Write a class complex to represent complex numbers, along with overloaded operators + and * which adds and multiplies them.
  2. Write a class vector representing a vector of n dimension. Overload the + and * operator which calculates the sum and the dot product of them.
  3. Write __str__() method to print the vector as follows:
7i + 8j + 10k

Assume vector of dimension 3 for this problem.

  1. Override the __len__() method on vector of problem 5 to display the dimension of the vector.
Was this article helpful?

Leave a Comment

Your email address will not be published. Required fields are marked *