Through Classes we can define many new data types. But suppose we have to define some datatype with some common properties. Then should we go about creating datatypes with bunch of repetitive properties for all of them, that will be wasteful job. Many built-in types have properties in common with other types. For example, types list and str each have len function that mean the same thing.
So how is this achieved?
Inheritance provides a convenient mechanism for building groups of related abstractions. It allows programmers to create a type hierarchy in which each type inherits attributes from the types above it in the hierarchy.
The class object is at the top of the hierarchy. This make sense, since in Python everything that exists at runtime is an object. Because Python inherits all of the properties of objects programs can bind a variable to a Person, append a Person to a list , e.tc.
A subclass inherits the attributes of its superclass in addition to this a subclass can :
For Example :
class human(object):
name = " "
def __init__(self, name):
self.name = name
def printName(self):
print "Name = " + self.name
class parent(human):
def __init__(self, name):
self.name = name
def work(self):
print "working"
class children(human):
def __init__(self, name):
self.name = name
def study(self):
print "studying"
So how is this achieved?
Inheritance provides a convenient mechanism for building groups of related abstractions. It allows programmers to create a type hierarchy in which each type inherits attributes from the types above it in the hierarchy.
The class object is at the top of the hierarchy. This make sense, since in Python everything that exists at runtime is an object. Because Python inherits all of the properties of objects programs can bind a variable to a Person, append a Person to a list , e.tc.
A subclass inherits the attributes of its superclass in addition to this a subclass can :
- add new attributes like class variables, instance variable and methods
- override attributes of the superclass. For example , if same method is defined in class and subclass, then the method defined in subclass overrides the method defined in superclass.
For Example :
class human(object):
name = " "
def __init__(self, name):
self.name = name
def printName(self):
print "Name = " + self.name
class parent(human):
def __init__(self, name):
self.name = name
def work(self):
print "working"
class children(human):
def __init__(self, name):
self.name = name
def study(self):
print "studying"
In the code above class human is the superclass and class parent and class children are called subclasses as they inherits from superclass human. Now when we create instance of class human and class parent :
latika = human("latika")
latika.printName()
at = parent("lat")
lat.printName()
lat.work()
will give output :
Name = latika
Name = lat
working
latika = human("latika")
latika.printName()
at = parent("lat")
lat.printName()
lat.work()
will give output :
Name = latika
Name = lat
working
No comments:
Post a Comment