This is a text field

Inheritance in python

     Python program to demonstrate Inheritance.  so,basically what is Inheritance?

     Inheritance give permission to user to not to start program  from scratch. The first class attribute can be come with help of Inheritance . Example, the first class is car, and second class is ElectricCar .the all car properties can come with help of Inheritance. In this car class is parent class,ElectricCar class is child class.

car.py

class Car():
    """Intialization
of car attributes
"""                 
    def __init__       (self,name,
model,year):
  
      # access the car
  # attribute
      self.name = name
      self.model = model
      self.year = year

    def describe_car(
        self):
        """print the
information    
        about car"""

       
print(self.name+" 
       "+self.model+" "
       +str(self.year))

class ElectricCar(Car):
      """Intialization
of car attributes
"""
      def__init__(self,
name,model,year):
"""Evoking the
class
attributes """
super().__init_
_(name,model,
year)

def display(self):
"""display
ElectricCar
model"""
print("Model:"+
self.model)

# creating a ElectricCar
# object

Vehicle = ElectricCar(
"tesla","S1M",
2016)

print(Vehicle.name)
Vehicle.describr_car()


Output:

tesla
tesla S1M 2016

Note: super() method
evoke the
attributes from
parent class to
child class.



Avatar for Vipul kunwar
Follow me

    Leave a Reply

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

    Inheritance in python

         Python program to demonstrate Inheritance.  so,basically what is Inheritance?

         Inheritance give permission to user to not to start program  from scratch. The first class attribute can be come with help of Inheritance . Example, the first class is car, and second class is ElectricCar .the all car properties can come with help of Inheritance. In this car class is parent class,ElectricCar class is child class.

    car.py

    class Car():
        """Intialization
    of car attributes
    """                 
        def __init__       (self,name,
    model,year):
      
          # access the car
      # attribute
          self.name = name
          self.model = model
          self.year = year

        def describe_car(
            self):
            """print the
    information    
            about car"""

           
    print(self.name+" 
           "+self.model+" "
           +str(self.year))

    class ElectricCar(Car):
          """Intialization
    of car attributes
    """
          def__init__(self,
    name,model,year):
    """Evoking the
    class
    attributes """
    super().__init_
    _(name,model,
    year)

    def display(self):
    """display
    ElectricCar
    model"""
    print("Model:"+
    self.model)

    # creating a ElectricCar
    # object

    Vehicle = ElectricCar(
    "tesla","S1M",
    2016)

    print(Vehicle.name)
    Vehicle.describr_car()


    Output:

    tesla
    tesla S1M 2016

    Note: super() method
    evoke the
    attributes from
    parent class to
    child class.



    Avatar for Vipul kunwar
    Follow me
      Back to top