Sitemap
NIRAJAN JHA

NIRAJAN JHA

I write about SQL ,Python and ML for beginners. Learn new contents every day with fun. If you find my articles helpful, feel free to follow.

Object-Oriented Programming(OOP)

2 min readSep 2, 2024
Press enter or click to view image in full size

In Python, object-oriented programming (OOP) is a programming paradigm or approach that uses objects and classes to design and organize code. In simpler terms, OOP is a way to structure programs by bundling related properties and behaviors into individual objects, which makes the code more modular, reusable, and easier to understand.

Key OOP Principles:

1. Abstraction — Expose only what is necessary and hide all unnecessary details. This allows us to focus on what an object does instead of how it does it.

Example: When we use a remote control, we don’t need to know how it sends signals to the TV; only thing we care about is pressing the right button to change the channel.

2. Encapsulation — Hide all unnecessary complex processing by packing all related data and methods (functions) into a single unit called a class. This helps in keeping the code clean and organized.

Example: Consider a car. Here, we interact with it using pedals and a steering wheel, but the complex internal workings (like the engine) are hidden from us.

Get NIRAJAN JHA’s stories in your inbox

Join Medium for free to get updates from this writer.

3. Inheritance — The process by which one class (child class) inherits properties and behaviors (methods) from another class (parent class). This allows for code reusability.

Example: If we have a class called Animal, a Dog class could inherit from it, meaning that Dog would automatically have the basic traits of Animal.

4. Polymorphism — The ability of different classes to be treated as instances of the same class through inheritance. It also refers to the ability to use a single method in different ways.

Example: Consider the concept of shape where a Circle and Rectangle are both shapes. A method draw() could be implemented differently for each shape but can be called in the same way.

What are Objects and Classes?

Class: A class is a blueprint or template for creating objects. It defines a set of attributes (data) and methods (functions) that the created objects will have.

Example: Suppose we want to create several cakes. We start with a recipe (class) that tells us the ingredients (attributes) and the steps (methods) needed to make a cake.

Object: An object is an instance of a class. Each object can have its own unique properties and can use the methods defined by its class.

Example: If Cake is the class, then chocolate_cake and vanilla_cake would be objects created from that class.

--

--

NIRAJAN JHA
NIRAJAN JHA

Written by NIRAJAN JHA

I write about SQL ,Python and ML for beginners. Learn new contents every day with fun. If you find my articles helpful, feel free to follow.