Fundamentals of OOPS
OOPS stands for Object-Oriented Programming System
It is a type of programming system that works on objects rather than the function which made the whole process a lot easier and consumes less space due to its reusability.
There are some basic terminology that one should know about moving forward in this concept -
What is an object?
The object is just a real-world entity with behaviors and attributes through which users can interact. We can also say that it is just an instance of a class.
What is a class?
A class is just a blueprint that contains some values and functions which are transferred to the object automatically upon its creation.
OOPS have many advantages but, there are some disadvantages too. We will discuss them in the next sections-
Advantages of OOPS
There are several advantages of using OOPS. Some of the main ones are-
- It makes reading, understanding, and maintaining code much easier than when it was written with functions. Thus reducing maintaining cost.
- Because of this solving problems becomes easy. No matter the difficulty, problems can be easily solved.
- It works on the principle of DRY — Don’t Repeat Yourselves as once an object has been made we can use it as many times as wanted, thus saving a lot of time and effort.
- It hides the codes which are not necessary with the help of data abstraction.
Disadvantages of OOPS
- It is not used when dealing with small problems as it is more suited for a complex or bigger problem.
- There is a sudden increase in problem-solving time.
- It requires proper planning and execution as a mistake can cost him very much.
- Since it requires proper planning and has to be built from the bottom, its execution rate is affected deeply as its processing speed is much lower than the procedural-based programs.
Pillars of OOPS
There are mainly four pillars on which this model stands
A process in which any piece of data or object acts differently in different situations is called polymorphism.
It is a combination of two words poly and morphs which means the ability to take different shapes.
Let’s take an example — a human can be a child of their parents and at the same time be the parent of his children, can be an employee of a company, and can be the employer for those working under him.
It is mainly of two types -
- Compile Time Polymorphism
- Run Time Polymorphism
Compile Time Polymorphism
It is also called static polymorphism and only occurs at the compile time.
It is mainly of two types but can vary with the language
- Function overloading
- Operator overloading
Run Time Polymorphism
As its name specifies, a process that occurs at the run time. It is also known as dynamic polymorphism.
As the name suggests, the ability to have some or all properties from other classes(parent). It is the main reason why the reuse of code is possible in OOPS.
A perfect example will be a human child that inherits some of its parents’ ability in its DNA.
There are five types of inheritance-
- Single Inheritance
- Multiple Inheritance
- Multi-Layer Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
In this inheritance of the properties of a class(parent) are only given to only one other class(child).
In this type of inheritance, the properties a class (child) inherited are from multiple classes (parent).
Note — it is not permitted in java language.
In this type of inheritance, properties from one class(parent) are inherited by another class(child) are further inherited by another class.
It can also be explained as a type of inheritance that has more than one parent class but at different levels, as shown in the diagram.
In this type of inheritance, a parent class has more than one child class.
For example — when twins are reborn, they inherit some of the properties of their parents.
A type of inheritance that is made of a combination of two types of inheritances is called a hybrid inheritance.
Limitations of Inheritance
- Requires more time to execute a program as it has to navigate different classes at different times.
- The parent and the child class have a are very tight bond which means that if any modifications have to be made in the program both parent and child class have to be modified accordingly
- It is very complex as already told before the parent and the child classes are tightly bound together, thus if not handled carefully may lead to some unexpected errors, and due to its complexity, finding the error may become the most difficult part.
It is one of the main reasons that OOPS is so popular as it hides the unnecessary part from the ones that do not need that.
For example — a mobile phone has many features like a camera, recorder, etc, but the user has no intention of knowing how they were made he just has to know how to use it or how it can solve my problem. Here the process of making such things that are not required by the user is hidden with the help of data abstraction.
It is a process in which all the necessary data are bound together and presented to the user in such a way that he cannot do anything other than using those functions and cannot use the rest of the data until and unless given permission to invoke the hidden data and this has no effect on its other function that it is performing by default.
It is a type of abstraction as it hides all the unnecessary stuff from the user by not allowing him access to it.