Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

Python - Class-Objects - Code

The document contains examples of different Python OOP concepts like classes, objects, methods, inheritance, polymorphism, and method overriding. It shows a basic class with methods, a class with an init method and attributes, counting objects, displaying class documentation, parameterizing a constructor, method overloading, method overriding in inheritance, and a simple inheritance example.

Uploaded by

Eswar Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Python - Class-Objects - Code

The document contains examples of different Python OOP concepts like classes, objects, methods, inheritance, polymorphism, and method overriding. It shows a basic class with methods, a class with an init method and attributes, counting objects, displaying class documentation, parameterizing a constructor, method overloading, method overriding in inheritance, and a simple inheritance example.

Uploaded by

Eswar Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# A simple example class

class Test:

# A sample method
def fun(self):
print("Hello")

Test().fun()

# Driver code
a = Test()
a.fun()

Test().fun()

------------------------------

# A Sample class with init method


# A Sample class with init method
class Person:

# init method or constructor


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

# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)

p = Person('Shwetanshu')
p.say_hi()

---------------------------------

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)

"This would create first object of Employee class"


emp1 = Employee("Zara", 2000)

"This would create second object of Employee class"


emp2 = Employee("Manni", 5000)

emp1.displayEmployee()
emp2.displayEmployee()
print ("Total Employee %d" % Employee.empCount)

print ("Employee.__doc__:", Employee.__doc__)


print ("Employee.__name__:", Employee.__name__)
print ("Employee.__module__:", Employee.__module__)
print ("Employee.__bases__:", Employee.__bases__)
print ("Employee.__dict__:", Employee.__dict__)

-------------
class Addition:
first = 0
second = 0
answer = 0

# parameterized constructor
def __init__(self, f, s):
self.first = f
self.second = s

def display(self):
print("First number = " + str(self.first))
print("Second number = " + str(self.second))
print("Addition of two numbers = " + str(self.answer))

def calculate(self):
self.answer = self.first + self.second
# creating object of the class
# this will invoke parameterized constructor
obj = Addition(1000, 2000)

# perform Addition
obj.calculate()

# display result
obj.display()

-----------------------
Overloading
------------
class A:

def stackoverflow(self, i=0):


print ('only method',i)

ob=A()
ob.stackoverflow(2)
ob.stackoverflow()

-------------------
Overriding
------------
Example
--------
class Parent: # define parent class
def myMethod(self):
print ('Calling parent method')

class Child(Parent): # define child class


def myMethod(self):
print ('Calling child method')

c = Child() # instance of child


c.myMethod() # child calls overridden method

----------------------

# A Python program to demonstrate inheritance


-------------------------
# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
class Person(object):

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

# To get name
def getName(self):
return self.name

# To check if this person is employee


def isEmployee(self):
return False

# Inherited or Sub class (Note Person in bracket)


class Employee(Person):

# Here we return true


def isEmployee(self):
return True

# Driver code
emp = Person("Geek1") # An Object of Person
print(emp.getName(), emp.isEmployee())

--------------

You might also like