Class is a crucial part of a type of programming called the Object-Oriented Programming (OOP). Programming itself has two common types: procedural programming (writing procedures and functions) and OOP (using classes and objects). OOP has several advantages over procedural programming:
First, let's learn what are classes and objects in OOP. Classes are templates for objects, while objects are instances of the class. For example, we have a basket of fruit containing apple, mango, and orange. The class is the 'fruit', while the objects are 'apple', 'mango', and 'orange'. In the class, there are also attributes and methods. To put it simply, the attributes are variables belonging to a class, while methods are functions belonging to the class.
Syntax:
class MyClass: #lines of code
Example:
class MyClass: #The class x = 5 #attributes
Syntax: object_name = class_name()
To access the attributes of the class, we have to use the dot syntax
(object_name.attribute).
Example:
class MyClass: #The class x = 5 #attributes myobj = MyClass() print(myobj.x) #the output is 5
__init__ function
Classes usually have the __init__ function.
This function is always executed when an object is being initiated.
The __init__ function is used to
assign value to object properties and other operations needed when an object is created.
Example:
class MyClass: def __init__(self, name, age): self.name = name self.age = age myobj = MyClass("John", "20") print(myobj.name) print(myobj.age)
__str__ functionThis function controls what should be returned when the class object is represented as a string.
Example:
class MyClass: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f"{self.name} is {self.age}" myobj = MyClass("John", "20") print(myobj)
We can declare methods inside of the class. And, to call the method, we use the same way as calling attributes (using the dot syntax).
Example:
class MyClass: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f"{self.name} is {self.age}" def greet(self): print(f"Hello, {self.name}!") myobj = MyClass("John", "20") myobj.greet()
Encapsulation ensures sensitive data hidden from users. Setting a class as private member in Python can be do implicitly by adding two underscores before a variable name.
Example:
class MyClass: def __init__(self, name, age): self.__name = name self.__age = age #the attribute (name and age) is now private
Inheritance means inheriting the attributes and methods from one class to another. We say that the class that inherits from another class is a derived class (child class), while the class being inherited from is the base class (parent class).
Example:
#parent class class Person: def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) #the child class class Student(Person): def __init__(self, fname, lname): Person.__init__(self, fname, lname) x = Student("Mega", "Kim") x.printname()
We can also use the super() function in the child class.
This function will automatically inherit all the methods and properties from its parent class.
Example:
#parent class class Person: def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) #the child class class Student(Person): def __init__(self, fname, lname): super().__init__(fname, lname) x = Student("Mega", "Kim") x.printname()
Polymorphism enables users to use methods to perform different tasks. This allows users to perform a single action in different ways.
Example:
class Pig: def __init__(self, name): self.name = name def animalSound(self): print("The pig says wee wee") class Dog: def __init__(self, name): self.name = name def animalSound(self): print("The dog says bow wow") mypig = Pig("Piggy") mydog = Dog("Doggy") mypig.animalSound() mydog.animalSound()