GRUEN INTERNATIONAL SCHOOLS, PORT HARCOURTOOP is a programming approach in which programs are organized around objects that combine data with operations that work on the data.
Objects can represent real or conceptual entities such as Student, Car, Book or BankAccount.
A class is a blueprint or template used to create objects. An object is an instance of a class.
A Student class might define name, age and score as attributes and display_result() as a method.
An attribute describes a property of an object. A method is a function associated with a class/object.
A constructor initializes an object when it is created. In Python, __init__() is commonly used as the constructor.
class Student:
def __init__(self, name, score):
self.name = name
self.score = score
def display(self):
return self.name + ' scored ' + str(self.score)
student1 = Student('Ada', 85)
print(student1.display())
This example defines a class, initializes attributes, creates an object and calls a method.
Encapsulation keeps related data and methods together and controls how data is accessed.
Inheritance allows a new class to derive characteristics or behavior from an existing class. Polymorphism means that the same operation can behave differently for different objects.
Abstraction focuses on important features while hiding unnecessary implementation details.