View of OOP Terminology,Constructor and Inheritance in Python
View of OOP Terminology
• Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
•
Class variable: A variable that is shared
by all instances of a class. Class variables are defined within a class but outside any of the class's
methods. Class variables are not
used as frequently as instance variables are.
•
Data member: A class variable
or instance variable
that holds data associated with a class and its objects.
•
Function overloading: The assignment of more than
one behavior to a
particular function. The operation performed varies by the types of objects or
arguments involved.
•
Instance variable: A variable that is defined
inside a method
and belongs only to the current instance
of a class.
•
Inheritance: The transfer of the characteristics of a class
to other classes that are derived from it.
An object comprises both data members
(class variables and instance variables) and methods.
•
Instance: An individual object of a certain
class. An object obj that belongs to a class Circle,
for example, is an instance
of the class Circle.
•
Instantiation: The creation
of an instance of a class.
•
Method : A special kind of function
that is defined in a class definition.
•
Object: A unique
instance of a data structure
that's defined by its class.
class Employee:
'Common base class for all employees' empCount = 0
def init (self,
name, salary): self.name = name
self.salary = salary Employee.empCount += 1
def displayCount(self):
print "Total
Employee %d" % Employee.empCount
def
displayEmployee(self):
print "Name :
", self.name, ", Salary: ", self.salary
Ex:
class MyClass: variable =
"blah"
def function(self):
print("This is a
message inside the class.") myobjectx = MyClass()
print(myobjectx.variable)
Creating Instance Objects
To create
instances of a class, you call the class using class name and pass in
whatever arguments its init method accepts.
"This would create first object of Employee
class" emp1 = Employee("Zara", 2000)
"This would create second object of Employee
class" emp2 = Employee("Manni", 5000)
Accessing Attributes
You access
the object's attributes using the dot operator with object. Class variable would be accessed
using class name as follows
−
emp1.displayEmployee() emp2.displayEmployee()
print "Total
Employee %d" % Employee.empCount
class MyClass: variable =
"blah"
def function(self):
print("This is a
message inside the class.")
myobjectx = MyClass() myobjecty = MyClass()
myobjecty.variable = "yackity"
# Then pring out both values
print(myobjectx.variable) print(myobjecty.variable)
---------------------------------------------------------------------------------
init :
In the init method, self refers to the newly created
object; in other class methods, it refers to the instance whose method was called
class A(object):
def
init (self): self.x = 'Hello'
def
method_a(self, foo): print self.x + ' ' + foo
self :
self represents
the instance of the class. By using the "self" keyword we can access the attributes
and methods
of the class in python.
init :
" init "
is a reseved method in python classes. It is known as a constructor in object
oriented concepts. This method called when an object is created from the class
and it allow the class to initialize the attributes of a class.
The init method is run as soon as an object of a
class is instantiated. The method is useful
to do any initialization you want to
do with your object. Notice the double underscore both in the beginning and at
the end in the name.
class Person:
def init (self, name):
self.name
= name def sayHi(self):
print 'Hello, my name
is', self.name
p
= Person('Swaroop') p.sayHi()
A constructor is a special type of method (function) that is called
when it instantiates an object using the definition found in your class. The
constructors are normally used to initialize (assign values) to the instance
variables. Constructors also verify that there are enough resources for the
object to perform any start-up task.
Creating a constructor:
A constructor is a class function that begins with double underscore
(_). The name of the constructor is always the same init ().
While creating an object, a constructor can accept arguments if
necessary. When you create a class without a constructor, Python automatically
creates a default constructor that doesn't do anything.
What is Inheritance
Inheritance is used to specify that one class will get most or all
of its features from its parent class. It is a feature of Object Oriented
Programming. It is a very powerful feature which facilitates users
to create a new class with a few or more modification to an existing
class. The new class is called child class or derived class and the main class
from which it inherits the properties is called base class or parent class.
The child class or derived class inherits the features from the parent
class, adding new features to it. It facilitates re-usability of code.
Syntax 1:
1. class DerivedClassName(BaseClassName):
2.
<statement-1>
3. .
4. .
5. .
6. <statement-N>
Syntax 2:
1. class DerivedClassName(modulename.BaseClassName):
2.
<statement-1>
3.
4. .
5. .
6. <statement-N>
Parameter explanation:
The name BaseClassName must be defined in a scope containing the
derived class definition. You can also use other arbitrary expressions in place
of a base class name. This is used when the base class is defined in another
module.
Python Inheritance Example
Let's see a simple python inheritance example where we are using two
classes: Animal and Dog. Animal is the parent or base class and Dog is the
child class.
Here, we are defining eat() method in Animal class and bark() method
in Dog class. In this example, we are creating instance of Dog class and
calling eat() and bark() methods by the instance of child class only. Since,
parent properties and behaviors are inherited to child object automatically, we
can call parent and child class methods by the child instance only.
1.
class Animal:
2.
def eat(self):
3.
print 'Eating...'
4.
class Dog(Animal):
5.
def bark(self):
6.
print 'Barking...'
7.
d=Dog()
8.
d.eat()
9.
d.bark()
1.
Eating...
2.
Barking...
Multilevel
inheritance is also possible in Python unlike other programming languages. You
can inherit a derived class from another derived class. This is known as
multilevel inheritance. In Python, multilevel inheritance can be done at any
depth.
class Animal
def eat(self)
print 'Eating...'
def eat(self)
print 'Eating...'
class Dog(Animal)
def bark(self)
print 'Barking...'
class BabyDog(Dog)
def weep(self):
print 'Weeping...'
d=BabyDog()
d.eat()d.bark()
d.weep()
def bark(self)
print 'Barking...'
class BabyDog(Dog)
def weep(self):
print 'Weeping...'
d=BabyDog()
d.eat()d.bark()
d.weep()
Comments
Post a Comment