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: 3
Let’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,self
refers to the instance of the class being created.self.name
sets thename
attribute of the object to the value passed during object creation.self.age = age
: Similarly,self.age
sets theage
attribute of the object.
3. Defining Methods
def bark(self):
: This defines a method namedbark
that belongs to theDog
class.return f"{self.name} says Woof!"
: This method returns a string with the dog’s name followed by “says Woof!”.self.name
accesses thename
attribute of the object.
def get_age(self):
: This defines a method namedget_age
that belongs to theDog
class.return self.age
: This method returns theage
attribute of the object.
4. Creating an Object
my_dog = Dog("Buddy", 3)
: This line creates an instance of theDog
class. TheDog
constructor (__init__
method) is called with the arguments"Buddy"
and3
. These values are assigned to thename
andage
attributes of the newDog
object, respectively.my_dog
is a reference to this new object.
5. Using Object Methods
print(my_dog.get_age())
: This line calls theget_age
method on themy_dog
object. The method returns the value of theage
attribute, which is3
, andprint
outputs 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.