First, what’s Object-Oriented Programming? the answer to that is, well you can google the definition and go through all the buzzwords extravaganza to learn that it is one of the programming paradigms out there. Okay now, what’s a programming paradigm?
“The term programming paradigm is the style or way of thinking about and approaching problems.”
OOP is one way to approach a problem but a problem can be approached with various phases so there exist other paradigms like Functional, Imperative and Logical programming paradigms. When it comes to choosing a programming paradigm, we consider the optimum way to approach and apprehend the problem and select which paradigm is most suitable for the scenario. Talking about OOP, Java, Python, Ruby, C++ well list goes on and on, are some languages that implement OOP, however very few languages implement a 100% (Pure) paradigm. A lot of languages will facilitate programming in one or more paradigms, lets consider Scala language, you can do imperative, object-oriented, and functional programming. This is good, why? because a single program can feature multiple paradigms and paradigms are not meant to be mutually exclusive.
Why Object-Oriented Programming? well this is built around the concept of objects, where we address the problem by subdividing it to manageable small divisions with the intention of improving code readability and re-usability. There are four core concepts in OOP which defines this paradigm, let’s dive into those now.
Abstraction is used to hide complexity from the users and show them the relevant information only. Imagine you want to drive a car, for that matter you do not need to know the complex internal structure and how everything works, we discard irrelevant or unimportant details. Here, we hide the internal implementation details using abstract classes or interface.
Why do we use abstraction? well most programs are often extremely large, where maintaining a large code base like this for years is difficult. Take your mobile phone, there are application layer, frameworks, libraries and kernel which work together to satisfy user needs but a typical user doesn’t need to know how it work, but by using only few buttons and a touch screen the user accomplishes wonders, that is the abstraction. Abstraction mechanism should hide internal underlying complexity. It should only reveal operations relevant for the other objects, which avoids repetitive codes.
Inheritance, well this is a very simple concept. This is where properties of one class can be inherited by the other. Like father and son. We reuse the common logic and extract the unique logic into a separate class. Advantage is implementation of the DRY (Don’t Repeat Yourself) programming principle and of course improves code re-usability.
There are few inheritance methods, one is Single Inheritance, where one class inherits the properties of another . Multilevel inheritance, where a class is derived from a class which is also derived from another class, like father from grandfather and son from father. Make note that Java doesn’t support Multiple Inheritance. Hierarchical Inheritance is where class has more than one child classes. And alas Hybrid inheritance which is a combination of multilevel and multiple inheritance.
Well, the name gives it away. We say Encapsulation is achieved when each object keeps its state private. Take a system where we set all variables to public, Any class in the system have the permission to access and to modify the variables, system wide access which eventually leads to design problems (plus violates Tell Don’t Ask principle and Flags over Objects principle ). Main goal of this is to archive data protection and increase code readability. How do we implement encapsulation? what we do is we set private fields and implement public getter and setter methods for those fields. It is that simple.
Okay now, What the heck is this? General idea is the state where an object can take the shape of many different forms. This is where same method tends to be multiple forms. What polymorphism does is, it gives a way to use a class exactly like its parent, so there’s no confusion with mixing types BUT each child class keeps its own methods as they are.
There are two forms of polymorphism in Java; overloading method and overriding method. Method Overloading is creating a method with the same name as an existing method in a class where we can create multiple specialized versions of methods in a class. Method overriding is overriding the definition of a super class method in its subclass.
Okay now, those are the four core concepts in OOP, but there are three other concepts called Association, Aggregation and Composition. Association means the act of establishing a relationship between two unrelated classes. Aggregation and Composition come under Association.
Aggregation: Consider Car class and Wheel class. Where Car needs a Wheel object to function. But we cannot say that the Wheel object has no significance without the Car Object. It can be used in a Bike, Bus or different modes. (relation – “Has a” ) Aggregation is a narrower kind of association.
Composition: let’s take a Car and an Engine, consider this engine as very special and specific only to this car. This is mutually dependent on each other relationship, since we call it Composition. (relation – “Owns” ) This is a stricter form of aggregation.
That is it for this article, stay tuned 🙂