OOPS concepts
Here I tried to put oops concepts in simple language with examples, lets have a look.
Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.
In simple terms: Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”. Create objects, re-use them throughout the program, and manipulate these objects to get results.
a) Class
The class is a group of similar entities. It is only logical component and not the physical entity. Class a blueprint for an object. It contains properties and methods/functions. Class doesn’t consume any space.
Example: Car class can have properties- speed while methods braking.
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
b) Object
An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An Object contains both the data and the function, which operates on the data.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 13;
myObj.myString = "Some text";
c) Method
A method is a collection of statements that perform some specific task and return result to the caller.
Methods are time savers and help us to reuse the code without retyping the code.
Example: calculatePrize()
d) Message Passing
Objects communicate with one another by sending and receiving information to each other. Message passing involves specifying the name of the object, the name of the function and the information to be sent.
e) Inheritance
It is the mechanism in which one class is allow to inherit the features(fields and methods) of another class. It’s creating a parent-child relationship between two classes. It provides code reusability. It is used to achieve runtime polymorphism.
class Animal {
// eat() function
// sleep() function
};
class Dog : public Animal {
// bark() function, we can use functions of Animal class here
};
f) Polymorphism
If one task is performed in different ways, it is known as polymorphism. Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism compile time polymorphism and runtime polymorphism.
Compile-time polymorphism is achieved by method overloading.
Example:
connect()
connect(int a)
connect(int a, String age)
Runtime polymorphism is achieved by method overriding
Example:
public class Addition {
func add() {}
}
public class Result: Addition {
override func add() {
//your code goes here
}
}
class MyClass {// function with 1 int parametervoid func(int x) {cout << "value of x is " << x << endl;}// function with same name but 1 double parametervoid func(double x) {cout << "value of x is " << x << endl;}// function with same name and 2 int parametersvoid func(int x, int y) {cout << "value of x and y is " << x << ", " << y << endl;}};
g) Abstraction
Abstraction is the concept of hiding the internal details and describing things in simple terms. Abstraction can be achieved in object-oriented programmings, such as encapsulation and inheritance.
Example: Withdraw money from ATM, user only knows to enter password and amount but knows the process happening in background.
class Adder {
public:
// constructor
Adder(int i = 0) {
total = i;
}
// interface to outside world
void addNum(int number) {
total += number;
}
// interface to outside world
int getTotal() {
return total;
};
private:
// hidden data from outside world
int total;
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() <<endl;
return 0;
}
h) Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. The data in a class is hidden from other classes, so it is also known as data-hiding.
Access modifier keywords are used for encapsulation in object oriented programming. i.e. Public, Private etc.
Example: a capsule, it is wrapped with different medicines.
class Encapsulation {private:
// data hidden from outside world
int x;public:// function to set value of// variable xvoid set(int a) {x =a;}// function to return value of// variable xint get() {return x;}};// main functionint main() {Encapsulation obj;obj.set(5);cout<<obj.get();return 0;
}
i) Association
Association is a relationship between two objects. It defines the diversity between objects. In this OOP concept, all object have their separate lifecycle, and there is no owner.
Example: consider class, many students can associate with one teacher while one student can also associate with multiple teachers.
There can be four types of association between the objects:
- One to One
- One to Many
- Many to One, and
- Many to Many
public class BlogAccount {
private BlogEntry[] blogEntries;
}
public class BlogEntry {
Int32 blogId;
string caption;
string text;
}
j) Aggregation
Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is ownership. It represents the weak relationship between objects.
Example: consider class, department and teacher. Here, a single teacher can’t belong to multiple departments, but even if we delete the department, the teacher object will never be destroyed.
public class BlogAuthor {
private Int32 authorId;
private string name;
}
public class BlogAccount {
private BlogEntry[] blogEntries;
}
k) Composition
A composition is a specialized form of Aggregation. It is also called “death” relationship. If you delete the parent object, all the child objects will be deleted automatically.
Example: School has-a Class Room. Here the room can’t exist without the School.
public class House {
private Room room;
public House() {
room = new Room();
}
}
l) Coupling
Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware of each other. If a class has the details information of another class, there is strong coupling.
m) Cohesion
Cohesion refers to the level of a component which performs a single well-defined task. A single well-defined task is done by a highly cohesive method. The weakly cohesive method will split the task into separate parts.
You would always strive for high cohesion & low coupling in OOP. For more details do visit this link.
Thanks for examples
https://www.infoworld.com/article/3029325/exploring-association-aggregation-and-composition-in-oop.html
https://www.tutorialspoint.com
https://www.geeksforgeeks.org
Thank you, Happy coding!