Object Oriented Programming Basics
We all wanted to learn more about object oriented programming, what it is and what makes it so great; in this post, we will cover the fundamentals.
First of all, what is OOP ? it is a programming paradigm based on the concept of “objects”, a car is an object that has attributes like color and weight and behaviours like transmitting shifts, we can represent this by a class car that has the car attributes and it’s behaviours (functions).
So, in this one short piece, I synthesized what I studied and learnt from other articles and books.
Basic Terminologies:
Object:
It’s the instance of a class it’s the working entity of a class.
Class:
It is a template or blue print about the capability of what an object can do.
Method:
The behaviour of a class. It tells what a method can do.
Instance:
Object and Instance both are same with small difference.
OOPs Concepts:
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
Encapsulation:
Encapsulation is a way for enclosing data (instance variables) and code operating on data (methods) as a single entity, similar to a Class.
The main purpose of encapsulation is you would have full control on data by using the code.
Encapsulation allows a class’s variables to be hidden from other classes and accessed only through the methods of its current class.
As a result, it is sometimes referred to as data concealing.
Abstraction:
Abstraction is the process of hiding implementation details from the user so that just functionality is provided.
In other words, the user will have the information on what the object does instead of how it does it.
Abstraction has 2 types:
- Control Abstraction.
- Data Abstraction.
Control Abstraction:
Control abstraction is one of the primary functions of programming languages. Computer devices understand operations at the most basic level, such as shifting bits from one spot in memory to another and adding two bit sequences. This is possible at a higher level thanks to programming languages.
Data Abstraction:
Data abstraction is the process of defining the behavior of a data structure.
In concrete implementations, data may be internally represented in a variety of ways.
So in a nutshell: Data abstraction refers to defining the behaviour of the data structure. Data may be internally represented in different ways in concrete implementations.
Example: Collection, ArrayList
- The difference between concepts of encapsulation and abstraction is that encapsulation is about the packaging of the class (like how data should be accessed (setters/getters) and what data should be accessed (access specifiers)), whereas abstraction more about what the class does for you at a conceptual level.
- Encapsulation is hiding unnecessary data in a capsule or unit and Abstraction is showing essential feature of an object
- Abstraction encompasses encapsulation, information hiding, and generalisation.
- abstraction is a form of generalisation: writing code that applies to more than one specific situation.
- Abstraction is where you focus on only those details that are important for your purpose.
- Inheritance is also an example of abstraction.
- Encapsulation leads to Abstraction
Inheritance:
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.
Types Of Inheritance:
- Single Inheritance
- Multiple Inheritance
- Multi-Level Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Polymorphism:
Polymorphism (from the Greek for “having various forms”) is the ability to attribute a distinct meaning or usage to something in different circumstances — particularly, to allow an entity such as a function or an object to have more than one form.
There is two types of polymorphism:
- Static Polymorphism
- Dynamic Polymorphism
Static polymorphism is polymorphism that occurs at compile time, and dynamic polymorphism is polymorphism that occurs at runtime (during application execution). An aspect of static polymorphism is early binding. In early binding, the specific method to call is resolved at compile time.
The work of polymorphism can be addressed in two different ways;
- Method Overloading
- Method Overriding
Method Overriding
Implements Runtime Polymorphism
This is used in situations where a method inherited by a child class from a parent class doesn’t fit in the child’s class. Meaning, we’d have to re-implement the method in the child class.
You want to change the behaviour or extend upon it. Same signature different functionality.
When the method signature (name and parameters) are the same in the superclass and the child class, it’s called Overriding.
Method Overloading
Implements Compile time polymorphism
Overloading, in programming, refers to the ability of a method to behave in different ways depending on the parameters that are passed to the method.
When two or more methods in the same class have the same name but different parameters, and even a different return type it’s called Overloading.
Overriding vs Overloading
- Overriding implements Runtime Polymorphism whereas Overloading implements Compile time polymorphism.
- The method Overriding occurs between superclass and subclass. Overloading occurs between the methods in the same class.
- Overriding methods have the same signature i.e. same name and method arguments. Overloaded method names are the same but the parameters are different.
- With Overloading, the method to call is determined at the compile-time. With overriding, the method call is determined at the runtime based on the object type.
- If overriding breaks, it can cause serious issues in our program because the effect will be visible at runtime. Whereas if overloading breaks, the compile-time error will come and it’s easy to fix.
Summary
And by that now you have the basics under your belt!
I hope that you enjoyed the article!