Day 11

Pramod Ray
3 min readSep 19, 2019

--

Oops (Object Oriented Programming): The basic idea is combine code with real entity object.

Object: Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc

An object has three characteristics:

  • State: represents the data (value) of an object.
  • Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
  • An object is a real-world entity.
  • An object is a run-time entity.
  • The object is an entity which has state and behavior.
  • The object is an instance of a class.

Class : A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can’t be physical.

A class in Python can contain:

  • Fields
  • Methods
  • Constructors

Method: Method are function define inside the body of class. They are used to define the behaviors of an object


def method1(self):
print("Pramod")

def method2(self,someString):
print("Software Testing:" + someString)


def main():
# exercise the class methods
c = my-class ()
c.method1()
c.method2(" Testing is fun")

Constructors : Class function that begins with double underscore(_) are called special function as they have special meaning.

The special function gets called whenever a new object of that class is instantiated. One particular interest is the init() function

class User:
name = ""

def __init__(self, name):
self.name = name

def sayHello(self):
print("Welcome to Tarzan-skills, " + self.name)

User1 = User("Pramod")
User1.sayHello()

Encapsulation: Encapsulation is a mechanism of Oops which provide to data member function. It is like a capsule which have binding of several method function, constructor and variable. It is also hiding the private details of a class and restrict access to method and variables. This prevent data from direct modification which is called encapsulation.

`Note: In Python have no private keyword. In python getter and setter are using for restricted the user access. 

Example 1.

class Robot(object):
def __init__(self):
self.__version = 22

def getVersion(self):
print(self.__version)

def setVersion(self, version):
self.__version = version

obj = Robot()
obj.getVersion()
obj.setVersion(23)
obj.getVersion()
print(obj.__version)

Abstraction: Abstraction is mechanism of oops which is using for hiding the unnecessary function and method.

Note :The difference between abstraction and encapsulation:
Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.

Inheritance: Inheritance is a feature used in object-oriented programming; it refers to defining a new class with less or no modification to an existing class. The new class is called derived class and from one which it inherits is called the base. Python supports inheritance; it also supports multiple inheritances. A class can inherit attributes and behavior methods from another class called subclass or heir class.

class myClass():
def method1(self):
print("Tarzan-skill")


class child-class(myClass):
def method1(self):
myClass.method1(self);
print ("child-class Method1")

def method2(self):
print("childClass method2")

def main():
exercise the class methods
c2 = childClass()
c2.method1()
c2.method2()

Polymorphism : The word polymorphism means having many forms. In programming, polymorphism means same function name (but different signatures) being uses for different types.

class India():   def capital(self):        print("New Delhi is the capital of India.")   def language(self):
print("Hindi the primary language of India.")
def type(self):
print("India is a developing country.")
class USA():
def capital(self):
print("Washington, D.C. is the capital of USA.")
def language(self):
print("English is the primary language of USA.")
def type(self):
print("USA is a developed country.")
obj_ind = India()
obj_USA = USA()
for country in (obj_ind, obj_USA):
country.capital()
country.language()
country.type()

--

--

Pramod Ray
Pramod Ray

No responses yet