We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13
Programming and Problem Solving
UNIT: VI Object Oriented Programming
Features of Object Oriented Programming
Features of OOP ▪ Class ▪ Object ▪ Methods ▪ Inheritance ▪ Polymorphism ▪ Data Encapsulation and Data Hiding Class ▪ Not possible to built real world data using Built in Data type ▪ OOP Specifically designed to solve real world problems ▪ A Class is used to describe something real world entity ▪ Class provides a template or Blueprint ▪ Consider Student Class Example Student can have attributes like Name, RollNo, Course, Result Operations that can be performed on this are AddData, DisplayData, EditData and so on. Class ▪ The operations and data is applicable to all students in class. ▪ A student from class is an object of class. ▪ We can create multiple objects for the same class ▪ Therefore Class is also called collection of objects. ▪ Class is user-defined data type. Class Example class student(): rollno=9999 #class data name=“xyz” #class data def __init__(self, rollno,name): #Object Method …Constructor self.rollno=rollno self.name=name def display(self): #Object Method print(self.rollno,” “, self.name) S = student(1101, “Archana”) #Creating Objects S.display() #Calling Class Function Objects ▪ Object is an instance of class ▪ According to previous example, all students of class are object of that class. ▪ Hence class can have multiple instances(objects) ▪ Every object has some data and functions associated with it. ▪ These functions stores data in variables and respond to message which it receives from other objects. ▪ Class is logical structure where object is physical actuality. Object Example class student(): --------------
S = student() #an object created
A = student() #one more object created Methods ▪ Function associated with class ▪ Defines operations ▪ Only methods of class can access data of class ▪ Every object can have different data associated with it. ▪ Python OOP contains three types of methods Constructor method Object Methods Class Methods Methods Example class student(): rollno = 1101 name = “Akash” def __init__(self) : #Constructor Method / Function def display(self) : #Object Method/Function def show() : #Class Method/Function Inheritance ▪ In inheritance we create new class from existing class ▪ New class called Child/Subclass/Derived class Parent Class Name, Roll_No ▪ This new class inherits attributes of parent class Add(), Display()
▪ Inheritance relation is also called “is a” relation
▪ Main advantage of inheritance is code reusability
Child Class Example class student(): _______
class books(student): # Inheritance student class inherited in books
class Polymorphism ▪ One Name many forms ▪ Different meaning to method in different context ▪ Example Operator Overloading 3 + 5 --------> Answer is 8 “3” + “5” ----> Answer is 35 Data Abstraction and Encapsulation ▪ In Data Abstraction only essential details are shown and implementation details are hidden. ▪ Example: Television , DVD Player ▪ In Data Encapsulation class data is packed together and hidden from outside access. It can be done by defining data private, public and protected.