3. Object Oriented Programming - Basics

Object-oriented Programming is a programming paradigm based on the concept of objects, which may contain data, in the form of fields, often known as attributes and code, in the form of procedures, often known as methods.

Object-oriented Programming languages are diverse, but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types.

The concept of Class:

In Object-oriented programming, we use classes to represent real-world things and scenarios, and we create objects based on these classes.

Definition: A class is a blueprint or design that defines the variables and the methods common to all objects of a certain kind.

  1. The data stored in its variables are known as attributes / fields.
  2. The code that can access this data to give the necessary output are called methods / functions .

So, we understood that a class is used to associate code with the data that it operates on.

Using a class reduces the complexity as the code increases.

An object is a specimen of a class. An instance is a specific object created from a particular class.
Creating an object from a class is called instantiation.

Example:
Suppose we need to build a class Car for Honda company.

The variables name, colour are the attributes of the class Car.

method get_Speed is defined inside the class Car which does some manipulation of the attributes and returns the output.

Honda_City is the instance/object of the class Car.

Sample Input:

class Car():
	"""A simple attempt to model a Car."""
	def __init__(self, name, colour):
		"""Initializing name, colour and model attributes."""
		self.name = name
		self.colour = colour
	def get_Speed(self):
		"""Setting the speed of the car."""
		print("Top speed of " + self.name + " is 150 km/hr") 

# Creating an instance/object called Honda_City
Honda_City = Car("Honda City","Red")
# Calling the method using the name of the instance
Honda_City.get_Speed() 

Sample Output:

Honda City is available only in Red colour.
Top speed of Honda City is 150 km/hr