Object Oriented programming or OOP’s concept in python is same as in any other language. Python is an object oriented language. The very same concept of classes and it’s objects as in Java can be implemented in Python. Let us recall the some of the basic terminology of Object Oriented Programming.
1. Class: A user defined data-type. It consists of set of attributes that describe the functionality of instances created to the class. Atrributes can be, data members or methods. Each of these are called using dot operation.
2. Class Variable: Some time we need to keep track of number of objects created for a particular class, in that class variables are useful. Hence, that particular data member should be common to all the instances of the class. Any instance can call the data member and can change it’s value which is reflected on every other instances.
3. Data Member: Unlike class variable it is called as instance vairable, because every instance will have it’s own data member. It can’t be accessed by the other instance of the class.
4. Methods: Each class will have it’s own set of methods which will operate on the data members of that instance and class variables also. These methods of instances are called using dot operation.
5. Function Overloading or Method Overloading: Defining two different operations for same function name is called as Function Overloading. Those two functions are differentiate by their arguments. Python does not support function overloading. The function which is defined first will be hidden and can’t be called.
6. Instance: Objects for particular class will have it’s own set of data members and methods accessing those data members. Eg: obj1. x is different from obj.2
7. Inheritance: The concept of reusing already defined class to produce more specialized classes is called as inheritance. Eg: We can inherited CAR class to create classes like JAGUAR, AUDI, PROSCHE. CAR class will define the basic requirements needed for a car(such as steering, 4 wheels etc). Child classes(JAGUAR, AUDI etc) will inherit CAR and will add more specifications.
Creating Classes:
class class_name(object): #define attributes and methods . .
Let us create a student class.
class Student(object): def __init__(self, name, age, std): self.name = name self.age = age self.std = std def displayDetails(self): print ("Name of student" + self.name) print ("Age of student: " + str(self.age)) print ("Student studying in class: " + str(self.std)) print () john = Student("John", 15, 10) john.displayDetails() john.name = "John willamson" john.displayDetails()
1. Creating Class: In the above code we have created simple class students which contains the attributes name, age, std and methods to display the details of student.
2. Creating Instances: Now, john is called as instances of the class. When ever an instance is created __int__ method of class is invoked automatically and instance variables name, age and std as assigned their particular values.
3. Accessing data members through instances: Now using dot operator we can call method displayDetails() to print the details of the particular student. We can also access data members using dot operator, here we have renamed “John” to “John willamson”.