What are OOP (Object Oriented Programming) Concepts?
What are the various OOP aka object oriented programming concepts. Could you describe them all?
- Encapsulation is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
- Abstraction is selecting data from a larger pool to show only the relevant details to the object. It does not focus on the background complexities.
- Polymorphism is the concept where an object behaves differently in different situations.
- Inheritance is a mechanism in which one class acquires the property of another class.
- Association defines a relationship between classes of objects that allows one object instance to cause another to perform an action on its behalf.
- Aggregation is a special form of association. It is a relationship between two classes like association however it is directional, which means it is strictly a one way association.
- Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
Object-Oriented Programming is a methodology to design a program using classes and objects. There are four main OOP concepts in Java.
It lets programmers create new classes that share some of the attributes of existing classes. It provides code reusability.
If one task performed by different ways, it is known as polymorphism. In Java, we use method overloading and method overriding to achieve polymorphism.
In Java, abstraction means simple things like objects, classes, and variables represent more complex underlying code and data. It lets avoid repeating the same work multiple times. In Java, we use abstract class and interface to achieve abstraction.
Binding code and data together into single unit.A java class is the example of encapsulation.
Object Oriented Programming has been defined pretty well in textbooks. The only thing that matters in practical application is how the programmer makes use of those objects. It's super easy to write bad code with OOP approach.
There's also a misconception that OOP is a better approach over the traditional functional programming approach. I beg to differ - OOP is a better approach when the overall complexity of the project grows. There's no point in opting for OOP if the overall code is just a few lines of code ( a few hundred, maybe).
In my opinion, OOP allows us to organise our code well so that it's maintainable. As the project grows bigger in size and complexity, you need reusable classes and methods and that's where you can make use of Inheritance, Polymorphism, Abstraction, Encapsulation et al.
However, the bottom line is that it all depends upon the programmer or developer. As I said - it's pretty easy to make the project super complicated to understand with BAD programming - be it OOP or Functional.
thanks for all your efforts.