Saturday, April 7, 2012

Overwrite Arguments of a Parent Class

Hello Everyone, In this Post I will Discuss how to overwrite the Variables or Arguments of a Parent Class when inherited.

From the earlier posts we already know how to make Classes, and how to Inherit a Parent Class.

Many a time, we inherit a Parent Class but we don't want some variables to be same as the parent class. So in this case we overwrite the variable. But the variable of the Parent Class remains unaffected.

Lets check out this simple example:

>>> class parentClass:
               car1 = "I am Mercedez Benz"
               car2 = "I am Maruti 800"
               car3 = "I am Honda City"


>>> class localDealer(parentClass): <--Parent Class Inherited
               car1 = "I am a Swift"  <--variable overwritten
               car3 = "I am a Zen"  <--variable overwritten
               car4 = "I am somecar" <-- A new variable created for the child class

>>> headDealer = parentClass() <--Object Created To use the Parent Class
>>> localCarDealer = localDealer() <-- Object Created to Use the Child Class
>>> localCarDealer.car1
'I am a Swift'
>>> localCarDealer.car2
'I am Maruti 800'
>>> localCarDealer.car3
'I am a Zen'
>>> localCarDealer.car4
'I am somecar'
>>> headDealer.car1
'I am Mercedez Benz'
>>> headDealer.car2
'I am Maruti 800'
>>> headDealer.car3
'I am Honda City'

So this was a Basic Stuff about the Overwriting variables of a Class. Practice it, and Dont Forget to check My next Post.

Thank You!


Post a Comment