Object and Class
Object and Class
Object and Class
They may represent a person, a place, a bank account, a table of data or any item that the
program must handle.
When a program is executed the objects interact by sending messages to one another.
- DropOff
- GoOnDuty
- GoOffDuty
- GetDriver
- SetDriver
- GetNumPassengers
What is a Class..?
A class is a special data type which defines how to build a certain kind of object.
The class also stores some data items that are shared by all the instances of this class
Instances are objects that are created which follow the definition given inside of the
class
Define a method in a class by including function definitions within the scope of the class
block
There must be a special first argument self in all of method definitions which gets bound
to the calling instance
class student:
“““A class representing a student ”””
def init (self , n, a):
self.full_name = n
self.age = a
def get_age(self):
return self.age
Define class:
Class name, begin with capital letter, by convention object: class based on
(Python built-in type)
Define a method
Like defining a function
Must have a special first parameter, self, which provides way for a method to refer to
object itself
A Simple Class def: Student
The first argument of every method is a reference to the current instance of the
class
By convention, we name this argument self
In init , self refers to the object currently being created; so, in other class
methods, it refers to the instance whose method was called
Similar to the keyword this in Java or C++
But Python uses self more often than Java uses this
You do not give a value for this parameter(self) when you call the method,
Python will provide it.
Syntax for accessing attributes and methods
It Possible to define two kinds of methods with in a class that can be called without an
instance
1) static method
2) class method
3) Instance method
Normally a class method is passed ‘self’ as its first argument. Self is an instance object
Some times we need to process data associated with instead of instances
Let us assume, simple function written outside the class, the code is not well associated with
class, can’t be inherited and the name of the method is not localized
Hence python offers us static and class methods
Static Method
class MyClass:
def my_static_method():
----rest of the code---
my_static_method=staticmethod (my_static_method)
Sample program For Static Method
class Students(object):
total = 0
def status():
print('\n Total Number of studetns is :', Students.total)
def __init__(self, name):
self.name= name
Students.total+=1
#Types of methods
#instance method
class c1:
def fun(self):
print('hello')
o=c1()
o.fun()
Static Method
#static method
class c2:
@staticmethod
def f1():
print('static method')
c2.f1()
Class Method
#class method
class c3:
@classmethod
def f2(cls):
print('class method')
c3.f2()
Encapsulation
The terms encapsulation and abstraction (also data hiding) are often used as
synonyms. They are nearly synonymous. abstraction is achieved though
encapsulation.
Data hiding and encapsulation are the same concept, so it's correct to use them
as synonyms
At least not directly, but they can be accessed through private name mangling.
That means, private data A can be accessed by the following name construct:
instance_name._classname A
Public, Protected and Private Data
>>> x = Encapsulation(11,13,17)
>>> x.public 11
>>> x._protected 13
>>> x._protected = 23
>>> x._protected 23
>>> x. private
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Encapsulation' object has no attribute '
private‘
>>>
The following table shows the different behavior Public, Protected and Private Data
Inheritance
The new class is called derived (or child) class and the one from which it inherits is called the
base (or parent) class.
Derived class inherits features from the base class, adding new features to it.
This results into re-usability of code.
Syntax:
class Baseclass(Object):
body_of_base_class
class DerivedClass(BaseClass):
• body_of_derived_clas
Inheritance
single level inheritance and acces Modifiers modifiers
class ch(c1):
pass
och=ch(1,2,3)
print(och.a)
print(och._b)
#print(och.__c)#error because c is private
print(och._c1__c)
Multiple Inheritance
•Syntax:
class Base1:
Statements
class Base2:
Statements
•Syntax:
class Base:
pass
class Derived1(Base):
pass
class Derived2(Derived1):
pass
MultiLevel Inheritance