What is Oops?
Oops is popularly known as an object-oriented programming language.
In Oops program is divided into objects.
The object is a real-world entity.
Any entity that has properties and behaviour is known as an object. In other words, it can have attributes and functions.
Oops consists of real-world entities and encourages programs to be divided into objects that are real-world entities.
Oops allows programmers to deal with real-world problems by using real-world entities known as objects.
Two Basic Components of Oops
Components and features of Oops is listed below one by one:
- Objects: it is a real-world entity. For example a door, pen, book pencil, or anything.
- Class: A class is a group of objects. They are also known as blueprints. It is a template from which we make objects.
Example:
If “student” is a class. Then x,y,z are the names of the students which are objects that come under the class “student”.
Pillars of Object-Oriented Programming
- Polymorphism
Polymorphism is the short abbreviation for poly means many forms and shapes. The object has many forms. For example, a man can be a husband, father, or son.
Types of Polymorphism
- Compile time Polymorphism
When multiple functions with the same names are present the compiler decodes. which function to call or run at compile time is called compile time polymorphism.
- It is also called Static polymorphism and early Binding.
- It is implemented using function overloading.
- No Inheritance is involved.
- The execution is faster as compared to run-time polymorphism.
- Runtime Polymorphism:
When multiple functions are present with the same name the compiler cannot decode which function to call or run. It is decoded at the runtime which functions to be executed so it is called Runtime polymorphism)
- It Is also called Dynamic polymorphism and Late Binding.
- It is implemented using function overriding.
- Inheritance concept is involved.
- The execution is slower as compared to compile time polymorphism.
Function Overloading and Function Overriding
Function Overriding
Function Overriding is a concept of polymorphism in which we define a function in Base (parent class) and the same function is redefined or extended in the derived class (child class). This concept of function overriding comes under polymorphism.
Function Overloading
This is defined as the same function name but having different parameters. This concept of function overloading also comes under polymorphism.
- Inheritance
Inheritance is one of the pillars of the oops which provides a code reusability feature. As the name one class inherits the property of another class or its superclass.
It has one base class and another called derived class.
Also called parent class and child class.
Types of Inheritance
Single-level Inheritance: when one child class inherits from one parent class.
Multiple Inheritance: when only one derived class is inheriting from more than one base class.
Multilevel/Multiline Inheritance: when a derived class inherits the property of the base class which also inherits from its base class.
Hybrid Inheritance: it is the combination of both multiple and hierarchical inheritance. It is also known as virtual inheritance.
Hierarchical Inheritance: In this case 1 parent class is there but the child classes are multiple.
- Encapsulation
It binds all data into a single unit.
For example, you can imagine it as a capsule a doctor gave you that encapsulates all medicine ingredients.
Under encapsulation comes
Getters and Setters
These are functions that are used to access private members under encapsulation.
Getters: This is used to get the name of the objects by returning the name of the objects.
Setters: This is used to set the name of the objects by returning the new name of the objects using the “this” keyword.
- Abstraction
Abstraction focuses on showing only essential details to the user and not showing unnecessary details.
It hides the complexity of the program.
For example: One does not think about the details of how the breaks are processed or manufactured by which company instead he/she just pushes the break whenever needed.
Some Important Terms
- Access modifiers
Access modifiers decide access to the particular class to data members or objects you can say.
There are basically 3 types of access specifiers:
- Public: anyone can access
- Private: no one can access other than class members because there private is written.
- Protected: If protected is written then only its child class can access parent class members but not allowed to others.
- Constructors
These are functions in oops which has the same name as the class name. It has no return type. It is only used with a public access modifier.
There are 3 types of constructor
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Destructor
- Destructor is a function of the class or a member function that is used with a public access modifier only.
- Destructor is written inside the class.
- It does not have any return type or void.
- It has same name as class name along with tilde sign in front (~)
- It is called automatically by the compiler when the object has been created.
- Use of destructor: Destructor is used to delete the object to save memory for other objects to be created in the program.
- For example: there are 4 objects created inside the class. All these 4 objects get deleted when they are no longer in use by the destructor. That means the destructor will be called 4 times here.
Conclusion
Object-oriented programming is incomplete without its pillars hope this blog has given you a deep idea of Oops pillars concepts. To know more about related keywords like static keywords, exception handling, and friend functions that are widely used in Oops, read out my previous blog “Simple Guide to Object-Oriented Programming” where all these terms are covered. Share your thoughts on how Oops helped you in programming.