Skip to main content

Inheritance in Python

Inheritance in Python
Inheritance, abstraction, encapsulation and polymorphism are the 4 fundamental concepts provided by OOP (Object Oriented Programming). Inheritance is a powerful feature of OOP that allows programmers to enable a new class to receive – or inherit all the properties & methods of existing class/classes. As we all came to know that class is a blueprint or template of an object. Every object is build from a class & the concept ‘inheritance’ is used to create relationship between these blueprints.
It is a feature of object-oriented programming which is used to define a new class with little or no modification to an existing class. The new class is called derived class or child class & the class from which this derived class has been inherited is the base class or parent class. Derived class is formed from base class plus it may include some extra additional features. This inheritance concept helps to reuse the code.
Inheritance

In the above diagram, the features of base-class are also present in the derived class along with the features of derived class. This base-class features can be accessible to derive class because of the concept of inheritance.

Syntax Of Inheritance In Python

Syntax:
class BaseClass1
# Body of base class

class DerivedClass(BaseClass1):
#body of derived - class

Three Ways Of Parent-child Class Interaction

When programmers use this type of object-oriented concepts and reuse codes, there are three ways a parent and a child class can interact with each other. These are:
  • Anything done to the child class imply an action on the parent class
  • Actions done on child-class overrides the actions done on parent-class
  • Anything done on the child-class alters the action done on the parent-class

Example Of Inheritance

Example:
#!/usr/bin/python

class Person:

def __init__(self, first, last):
self.firstn = first
self.lastn = last

def Name(self):
return self.firstn + " " + self.lastn

class Emp(Person):

def __init__(self, first, last, staffnum):
Person.__init__(self,first, last)
self.staffno = staffnum

def GetEmp(self):
return self.Name() + ", " + self.staffno

a
= Person("Alex", "Karlos")
b
= Emp("Alex", "Karlos", "A102")

print(a.Name())
print(b.GetEmp())
Output:
Alex Karlos
Alex Karlos, A102
In the above case, the object of derived class is created and is used to invoke both for the functions of base-class as well as derived class using a dot (.) operator. Here the result is not a part of derived class. When the interpreter is not found in the class (derived) whose object is defined, then it continues checking that attribute in the base class. This process continues in a recursion if the base – class is itself a derived from another class.

Implicit Inheritance

Implicit actions occur in Python Inheritance when a programmer defines a function in the parent but not in the child. This type of inheritance is shown using a simple example below:
Example:
#!/usr/bin/python
class super (object) :

def implicit(self) :
print "Super-Class with Implicit function"

class sub(super) :
pass

su
= super()
sb
= sub()

su
.implicit()
sb
.implicit()
Output:
Super-Class with Implicit function
Super-Class with Implicit function
In the above code both objects of base class as well as derived class can invoke the function of base class. Also, the ‘pass’ statement under the ‘sub’ class is used to tell Python that the programmer wants an empty block which is created under ‘sub’ class but it says there is nothing new to define in it.
The above program also shows that – if we put any function in the base-class (here ‘super’), then all the derived-class (here class ‘sub’) will also get the features automatically from the base-class i.e. inherit all the behavior from the parent-class

Comments

For Programs Click Here

Popular posts from this blog

Python Lists

Python Lists Dealing with data in a structured format is quiet generous, and this is possible if those data are set accordingly in a specific manner. So, Python provides these data structures named ‘lists’ and ‘tuples’ that are used to organize data in  single set. Python has 6 built-in sequences and among them the most famous are “lists and tuples”. The lists are containers that hold a number of other objects in a given order. It usually puts into practice the sequence protocol and allows programmers to add or remove objects from that sequence. Each element of the sequence is assigned a number i.e. he index and the first index is 0 (zero). This versatile data-type of Python is written in a sequence of list separated by commas between expressions. Creating Lists To build a list, just put a number of expressions in square brackets. The syntax is: Syntax: lst1 = [ ] # lst1 is the name of the list lst2 = [ expression1 , …. , expression_N ] Example: #!/usr/bin/python ls...

C++ Qualifiers and Storage Classes

C++ Qualifiers and Storage Classes Qualifiers and storage class are smaller but important programming concept that helps to make the quality of a variable more accurate for using that variable within the program. In this chapter you will learn about how qualifiers are used with variables and what the roles of different storage classes in C++ are. What is a Qualifier? A qualifier is a token added to a variable which adds an extra  quality , such as specifying volatility or constant-ness to a variable. They act like an adjective for a variable. With or without a qualifier, the variable itself still occupies the same amount of memory, and each bit has the same interpretation or contribution to the state/value. Qualifiers just specify something about how it may be accessed or where it is stored. Three qualifiers are there in C++. These are: const : This is used to define that the type is constant. volatile : his is used to define that the type is volatile. mutable:  applies to non...

C++ Objects and Classes

C++ Object Oriented C++ Objects and Classes In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language; this class describes the data properties alone. In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects. What is a class? A class is an abstract data type similar to ‘ C structure ‘. Class representation of objects and the sets of operations that can be applied to such objects. Class consists of Data members and methods. Primary purpose of a class is to held data/information. This is achieved with attributes which is also know as data members. The member functions determine the behavior of the class i.e. provide definition for supporting various operations on data held...