What is Object Oriented Programming (OOP)?
Object Oriented Programming (OOP) is a programming paradigm based on the concept of “objects,” which can contain data and code. The data is in the form of fields (often known as attributes or properties), and the code is in the form of procedures (often known as methods).
Why Use Object Oriented Programming?
OOP makes it easier to organize and manage complex software by grouping related tasks and data together into objects. It helps in:
- Modularity: Code is divided into smaller, manageable parts.
- Reusability: Objects and classes can be reused across different programs.
- Maintainability: Easier to update and maintain code.
- Scalability: Makes it easier to manage larger projects.
Basic Concepts of Object Oriented Programming
Classes and Objects
- Class: A blueprint for creating objects (a particular data structure), defining initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
- Object: An instance of a class. When a class is defined, no memory is allocated until an object of that class is created.
Methods
Methods are functions defined inside a class that describe the behaviors of the objects created from the class.
Attributes
Attributes are variables that hold data associated with a class and its objects.
Example of Object Oriented Programming:
Python:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return f"{self.name} says Woof!"
def get_age(self):
return self.age
# Creating an object of the Dog class
my_dog = Dog("Buddy", 3)
print(my_dog.get_age()) # Output: 3
Javascript:
class Dog {
constructor(name, age) {
this.name = name;
this.age = age;
}
bark() {
return `${this.name} says Woof!`;
}
getAge() {
return this.age;
}
}
// Creating an object of the Dog class
let myDog = new Dog("Buddy", 3);
console.log(myDog.getAge()); // Output: 3Let’s break down and explain the code to understand how it demonstrates Object Oriented Programming (OOP) concepts.
1. Defining a Class
class Dog:: This line defines a class namedDog. A class is a blueprint for creating objects. It defines a type of object, with its properties (attributes) and behaviors (methods).
2. The __init__ Method (Constructor)
def __init__(self, name, age):: This is the constructor method in Python. It’s called when a new instance (object) of the class is created. The__init__method initializes the object’s attributes.self.name = name: Here,selfrefers to the instance of the class being created.self.namesets thenameattribute of the object to the value passed during object creation.self.age = age: Similarly,self.agesets theageattribute of the object.
3. Defining Methods
def bark(self):: This defines a method namedbarkthat belongs to theDogclass.return f"{self.name} says Woof!": This method returns a string with the dog’s name followed by “says Woof!”.self.nameaccesses thenameattribute of the object.
def get_age(self):: This defines a method namedget_agethat belongs to theDogclass.return self.age: This method returns theageattribute of the object.
4. Creating an Object
my_dog = Dog("Buddy", 3): This line creates an instance of theDogclass. TheDogconstructor (__init__method) is called with the arguments"Buddy"and3. These values are assigned to thenameandageattributes of the newDogobject, respectively.my_dogis a reference to this new object.
5. Using Object Methods
print(my_dog.get_age()): This line calls theget_agemethod on themy_dogobject. The method returns the value of theageattribute, which is3, andprintoutputs this value to the console.
The Dog class defines the structure for dog objects, with attributes name and age, and methods bark and get_age. We create a Dog object named my_dog with the name “Buddy” and age 3, and use the get_age method to retrieve the dog’s age. This basic example illustrates how OOP organizes code into reusable, modular units.
Understanding the Real-World Analogy
Think of a class as a blueprint for a house. You can create many houses (objects) from this blueprint, each with its own unique attributes (like color, size) but all sharing the same structure.
Key Takeaways
- Class: Blueprint for creating objects.
- Object: An instance of a class.
- Attributes: Data associated with a class and its objects.
- Methods: Functions defined inside a class that describe the behaviors of the objects.
Conclusion
We have learned about the basics of Object Oriented Programming, including classes, objects, attributes, and methods. We have also seen examples in both Python and JavaScript to understand how OOP concepts are implemented in different programming languages.






