we will learn about the four main principles of Object Oriented Programming (OOP). These principles are like the rules that help us organize our code and make it easier to understand and use. The four main principles are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
1. Encapsulation
Encapsulation is like a capsule or a box that keeps everything together. It means that we bundle the data (attributes) and the methods (functions) that work on the data into a single unit called a class. Encapsulation helps protect the data from outside interference and misuse.
Example in Python:
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def display_info(self):
return f"{self.year} {self.make} {self.model}"
Example in Javascript:
class Car {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
displayInfo() {
return `${this.year} ${this.make} ${this.model}`;
}
}
In these examples, the Car
class bundles the attributes make
, model
, and year
with the method display_info
(or displayInfo
in JavaScript). This keeps everything related to the car in one place.
2. Inheritance
Inheritance is like a child inheriting traits from their parents. In programming, a class (called a child class) can inherit attributes and methods from another class (called a parent class). This helps us reuse code and make it more organized.
Example in Python:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Animal sound"
class Dog(Animal):
def speak(self):
return "Woof!"
Example in JavaScript:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return "Animal sound";
}
}
class Dog extends Animal {
speak() {
return "Woof!";
}
}
In these examples, the Dog
class inherits from the Animal
class. This means the Dog
class has all the attributes and methods of the Animal
class, but it can also have its own methods, like speak
.
3. Polymorphism
Polymorphism means “many forms.” It allows objects of different classes to be treated as objects of a common parent class. The main benefit of polymorphism is that it allows us to use a single method or function in different ways.
Example in Python:
class Cat(Animal):
def speak(self):
return "Meow!"
animals = [Dog("Buddy"), Cat("Whiskers")]
for animal in animals:
print(animal.speak())
Example in Javascript:
class Cat extends Animal {
speak() {
return "Meow!";
}
}
let animals = [new Dog("Buddy"), new Cat("Whiskers")];
for (let animal of animals) {
console.log(animal.speak());
}
In these examples, both Dog
and Cat
classes have a speak
method. Even though the speak
method does different things in each class, we can call speak
on any object in the animals
list without worrying about the specific type of animal.
4. Abstraction
Abstraction means hiding the complex details and showing only the essential features of the object. It’s like using a TV remote: you don’t need to know how the TV works internally; you just need to know which buttons to press.
Example in Python:
class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
Example in Javascript:
class Shape {
area() {
throw "Area method not implemented";
}
}
class Rectangle extends Shape {
constructor(width, height) {
super();
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
In these examples, Shape
is an abstract class with a method area
that doesn’t do anything (pass
or throws an error). The Rectangle
class inherits from Shape
and provides its own implementation of the area
method. This way, we can create other shapes (like circles, triangles) with their own area
methods, without changing the Shape
class.
Summary of Core Principles of Object Oriented Programming
- Encapsulation: Bundles data and methods together in a class.
- Inheritance: Allows a class to inherit attributes and methods from another class.
- Polymorphism: Allows methods to do different things based on the object calling them.
- Abstraction: Hides complex details and shows only the essential features.
Conclusion
In this chapter, we have learned about the core principles of Object Oriented Programming: encapsulation, inheritance, polymorphism, and abstraction. These principles help us organize our code, make it reusable, and easier to understand.