Python inheritance

Inheritance is one of the OOP(Object Oriented Programming) Concept. It involves a parent and child class.

A class is created and its member functions are defined. This class is called base class or parent class. When you want a copy of base class, you can inherit it  and create a child class.

A child class is simply a replica of base class.

1)Here, a base class is created with name " customer"

class customer:

  def __init__(cust, fname, lname):

    cust.firstname = fname

    cust.lastname = lname

    def printfullname(cust):

    print(cust.firstname, cust.lastname)

#Use the customer class to create an object, and then execute the printfullname method:

x = customer("jeev", "anand")

x.printfullname()

 

2) A child class “Regularcustomer” is created.

#Creating a child class Regularcustomer

class Regularcustomer(customer):

  pass

# pass denotes this class doesnot have any definition.

3)The variables and member functions are included in child class “Regularcustomer”

#The child class access the base class function printfullname()

  y = Regularcustomer(“Jey” ,”anth”)

  y.printfullname()

 when you execute this program, you get the following output.

 


How to override the child class function:

If you want to override the child class function,add the following code.

class Regularcustomer(customer):

   def __init__(cust, fname,lname):

      customer. __int__(cust,fname,lname)

If you want to add new member functions, use super() method.

 class Regularcustomer(customer):

    def __init__(cust, fname,lname):

       super(). __init__(fname,lname)

 #Add a new property age

       cust.age=25

       z= regularcustomer(" raje " , " eswari " , 25)

when you a new member function, the code will be

class Regularcustomer(customer):

    def __init__(cust, fname,lname,age):

       super(). __init__(fname,lname)

       cust.currentage=age

def message(cust):

    print(“Happy birthday”,cust.fitstname,cust.lastname,” for the year of”,cust.currentage)

when you execute this,you get the following output.


This is the way of using inheritance in python is explained with simple example of customer and regular customer.



Object oriented concepts -Classes and objects

 Everything is an object in real world. In object-oriented programming, basic term is classes and objects. Class is a collection of objects and methods. Each class has some properties and functionalities.

For eg, A car is a class. Name of the car is like the classname. Brand of the car, color and size are the properties of the class. Running is the functionality of a car. It is the method.

Let us create a class in python. Here, the code begins…

Syntax:  class classname:

                   Variablename

Eg: class SampleClass:

               S = 10

Next, an object is created. The object accesses the Sum variable and it gets printed.

        Ob = SampleClass()

        Print(Ob.sum)

Add this code in a file “SampleClass.py”. While executing this, we get the following output.

Next, Average of three numbers using class is given below.

AvgClass.py

class AvgClass:

  a = 10

  b = 20

  c = 30

ob = AvgClass()

print((ob.a+ob.b+ob.c)/3)

Executing this code, it will give the Average of the three numbers as output.


 This is the first concept in object oriented programming.