Inheritance & more on OOPs

Inheritance & more on OOPs

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

Syntax:

class Emoloyee:     #Base Class

          #Code

class Programmer(Employee):    #Derived or child class

          #Code

Copy

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

Copy

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:

@classmethod

def (cls, p1, p2):

          #code

Copy

@property decorators

Consider the following class

class Employee:

          @property

          def name(self):

                    return self.ename

Copy

if e = Employee() is an object of class employee, we can print (e.name) 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:

@name.setter

def name(self, value):

          self.ename = value

Copy

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)

Copy

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)

Copy

Chapter 11 – Practice Set

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.

4.     Write a class complex to represent complex numbers, along with overloaded operators + and * which adds and multiplies them.

5.     Write a class vector representing a vector of n dimension. Overload the + and * operator which calculates the sum and the dot product of them.

6.     Write __str__() method to print the vector as follows:

7i + 8j + 10k

Copy

Assume vector of dimension 3 for this problem.

7.     Override the __len__() method on vector of problem 5 to display the dimension of the vector