Sun classing - Python

A subclass is a class that inherits attribute names and methods from another class—the operation is called subclassing.

A base class (superclass) is defined as a class that another class inherits attributes from. Base classes are listed in parentheses in a subclass header. You have to separate base classes by putting commas between them, within the parentheses.

When you create a subclass, you can add or overwrite any methods of its base classes.

Python classes can be created:
lFrom scratch
>>>class A:
… pass

lBy using single inheritance
>>>class B(A):
… pass

lBy using multiple inheritance
>>>class D(B,C):
… pass

For a conceptual standpoint, take a look at the following example

Where,
Base class = writing tools
subclass = pen
subclass = chalk

Both subclasses pen and chalk inherit characteristics of the base class writing tools. The subsequent class defines a complex class called Employee.

class Employee:
def __init__(self,name,salary=0):
self.name = name
self.salary = salary
self.family = []
defraisesalary(self, percent):
self.salary = self.salary + (self.salary * percent)
def work (self):
print self.name, "writes computer code."
defhasfamily(self):
returnlen(self.family) == 0 # returns a boolean result
defaddmember(self, x):
self.family.append(x)
defremovemember(self, x):
iflen(self.family) > 0:
x = self.family[-1]
delself.family[-1]
return x

The next class is a subclass of the Employee class.

class Person(Employee):
"this is the class Person"
def __init__ (self, name):
Employee.__init__ (self, name, 50000)
def work (self):
print self.name, "works like any other employee."

Inherited methods of base classes aren't automatically called. It is necessary to call them explicitly. That's why, in the previous example, the Person.__init__ method had to call the Employee.__init__ method.

It is always necessary to pass the self argument because base classes don't know what instance is being used. The previous example passes three parameters to the base class's __init__ method (the self reference, an argument, and a default value for the other argument).

Multiple inheritance is defined by entering multiple classes in the header of a new class. The order used for informing the base classes really does matter. The precedence order, for a search in the base classes, starts at the classes located at the left side.

class A:
pass
class B(A):
pass
class C:
pass
class D(B,C):
pass

The precedence order for class D inheritance is: B, A, C.

>>>class A:
…def __init__(self, name):
… self.name = name
…defprintname(self):
… print 'The name %s belongs to class A!'% self.name

>>>class B(A):
…__baseclass=A
…def __init__(self, name):
… self.__ baseclass.__init__(self,name)
…defprintname(self):
… print 'The name %s belongs to class B!'% self.name
… self.__ baseclass.printname(self)

>>>class C(B):
…__baseclass=B
…def __init__(self, name):
… self.__ baseclass.__init__(self,name)
…defprintname(self):
… print 'The name %s belongs to class C!'% self.name
… self.__ baseclass.printname(self)

>>>A("monkey").printname()
The name monkey belongs to class A!
>>>B("parrot").printname()
The name parrot belongs to class B!
The name parrot belongs to class A!
>>>C("ant").printname()
The name ant belongs to class C!
The name ant belongs to class B!
The name ant belongs to class A!

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Python Topics