Object Oriented Programming Principles

What is OOP?

Object Oriented Programming which is simply known as OOP is a programming paradigm based on the concept of Objects rather than functions and logic. That's why it is called as Object Oriented Programming because this programming paradigm is based on the concept of Objects. 


❄️ So, this proves that OOP is nothing but another kind of a Programming Paradigm. There are several programming paradigms and they are simply; 
  1. Unstructured Programming - The most primitive of all programming paradigms. same code is repeated through the code. Have a sequentially flow of control.
  2. Structured Programming - Has non-sequentially flow of control. Uses functions to prevent repeating the same code again and again.
  3. Object Oriented Programming - Combines Data and Operations/Actions together. As the code is based on Objects it improves code re usability.

OOP Concepts

  1. Object
  2. Class
  3. Encapsulation
  4. Inheritance
  5. Polymorphism
  6. Abstraction
Encapsulation, Inheritance, Polimorphism, Abstraction are known as Key-Concepts of OOP as having a sound knowledge in above OOP concepts allows you to understand any OOP Language easily.

Usage of Object Oriented Programming (OOP) 

As mentioned earlier Object Oriented Programming is a  widely used programming paradigm for develop software solutions. So, this programming paradigm is based on Objects.

1. So, firstly let's discuss what is an Object?
"Object refers to a particular instance of a Class" or just simply Object is an instance of a Class.

2. So, again what is a Class?
"Class is an extensive program-code-template for creating Objects" or just simply Class is a template of an Object.

So, now you can see that there is a connection between these two OOP concepts. So, let's take an example a Car Class (Creating a Class called Car). So, in real world car has different features such as Color, Brand, Category, Fuel Type etc.. So, our Car Class also include those kind of features. In programming we call them as Properties, Attributes or Instance Variables of the Class.


So, here is our Car Class. I decided to use pure JavaScript to explain OOP because anybody can understand and anybody can test and run the code easily without installing any software or no need of any programming knowledge. If you wish to practically experience what is OOP, please open up your favorite web browser and open a new tab and then right click and select inspect. Then you'll show Developer tools, and switch to Console tab as show in the below screen. Then follow the below code.




So, in our Car Class I've used one of OOP  CORE concepts called "Encapsulation" to create this class which we going to discuss now.

3. Encapsulation is a data hiding technology. It completely restricts directly accessing/manipulating the attributes of the class and only allows to access or manipulate them by using getters and setters. Here you can see for each attribute there is a related get function(to retrieve the value of the attribute) and a set function(to manipulate attribute values)

So, that's about our Car Class. Now let's move on and check out how to create different Objects by instantiating that Car Class.


So, here we have created two objects of Car class.

First object is created by using the Default Constructor/ No Args Constructor of the Car class.
Second object is created using the Full Args Constructor/ All Args of the Car class.

So, I hope that you can understand the concept of  Default Constructor and Full Args Constructor.

As you can see in the above example Creating an object using Default Constructor doesn't affect/ change/ manipulate the values of the Attributes (also known as Properties or Instance Variables) of the Car class. You can see the result of that object as displaying as "undefined" for all the attributes of the Car class.

Note: value "undefined" is the default value for any variable in JavaScript

Also, you can see that Creating an Object using Full Args Constructor has been affect/change/ manipulate the all the Attributes (also known as Properties or Instance Variables) of the Car class. You can see the default value(default value is "undefined") of those attributes are replaced/ affected by the values I've defined when creating the second Object.

4. Inheritance is an OOP concept that is about the process of inheriting some features of Parent Class (also known as Super Class) to the Child Class (also known as Sub Class). So, using this concept we can access the Methods (also known as Functions or Behaviors) of the Parent Class from a Child Class instance.

Note : But we can't access all the methods of the Parent Class but only defined methods saying that these methods can be access outside of this Parent Class. We call them as Access Modifiers.

Eg:-  These are the Access Modifiers we can use in Java Language
  1. Private - Can only be accessed during the same Class (Simply these methods are "Private"). 
  2. Public  - Can be accessed from anywhere (Simply these methods are "Public").
  3. Protected - Within the Same Class, Within the Same Package, Outside the Package but only through Child Classes.
  4. Default - Can Only access inside the Same Package and Same Class.

I'll create a new Class called "Vehicle" and it has an attribute called "vehicleType" which describes the vehicleType. Ex :- Car, Van, Bus etc..


So, after Creating the Vehicle class we use the "extends" keyword to apply the concept of Inheritance
Note: class Car extends Vehicle means features of the class Vehicle (Parent Class) Inherits to the Car Class(Child Class).

So, let's create an Object of class Car type and see how the Inheritance works.


So, I hope that the above example gives you a clear idea about the concept of Inheritance.
We have create the Object of class Car type and class Car doesn't have any getter, setter, or any attribute called "vehicleType". But how we able to access those methods of class Vehicle from an instance of class Car because that's what Inheritance does. It inherits features of the Parent class to the Child class. So, we can access those methods of Vehicle class through a Car instance thanks to the concept of Inheritance.

5. Polymorphism is another OOP concept. This word has been made with a combination of two words Poly + morphic. In this word, Poly means many and morphic means forms.
So, this concept uses a single method call statement to represent multiple different forms/ types.

Polymorphism can be divided in to two main parts. They are;
  1. Compile-time polymorphism (also called as Static-binding, Static-polymorphism)
  2. Run-time Polymorphism (also called as Dynamic-binding, Dynamic-polymorphism)
Compile-time polymorphism

Compile-time polymorphism allows us to implement multiple methods within the same class using the same name but different set of parameters (different Signature). It is called as Method Overloading. So, in here Compile-time polymorphism is the Concept and the Method Overloading is the mechanism we use to perform that concept. This is called as Compile-time polimorphism because this allows the compiler (Eg:- For Java it's javac) to determine which method to be executed at the Compile-time.

Note : - 
Signature is a combination of method name and method parameter list (Signature = methodName + parameterList)

Note:- Javascript doesn't support Method Overloading instead it supports Method Overriding which only considers the last method with the same name. You can see that from the below example.


Therefore, to explain this concept I'll use Java as Programming Language.








Run-time Polymorphism

Run-time Polymorphism allows us to override a method of Parent class from the Child class. So, we can customize or completely replace the behavior of that method. In Run-time Polymorphism both methods in the Parent class and Child class have the same name and set of parameters (same Signature). Unlike Compile-time polymorphism, this doesn't allow the compiler to determine the executed method. Instead, the Interpreter (Eg:- For Java it's JVM-Java Virtual Machine) needs to determine which method to be executed at the Run-time.









6. Abstraction

Abstraction is an implementation hiding technique which frequently used in Object Oriented Programming. To perform the concept of  "Abstraction" programming languages generally use fully abstract classes called "Interfaces". Since we have cleared out the concept of class, only we have to figure out what is "Abstract Class" means. 

So, in general classes, there are methods with a method body that are defined for the purpose of manipulating the attributes of the particular class. So, in the general classes, there can't be any method without any implementation (which is also called Abstract Methods). 

But however, Interface is a fully Abstract class that may only contain Abstract Methods (method body less methods).

So, this concept is all about hiding the logic implementation from the client and only expose the working functionality/result to the user. This concept is mainly used in backend Service endpoint creations which defines all the abstract methods in a particular Interface without exposing the logic implementation and the client-side directly communicates with that abstract interface without knowing the operations doing for generating the resulting output. So, the client directly communicates with that service interface, and the implementation of each method is defined in a separate implementation class that doesn't directly expose to the outside. 

And further Abstractions allow keeping the method implementation hierarchy clear by not allowing to change those methods since it may break the system's workflow. So, in the implementation class you must definitely overide all the methods defined in the interface and should implement the method implementation for each method. Otherwise programming language will display an error by mentioning to overide all the defined methods in the Interface.

The below example will show a simple usage of the Abstraction.






Keywords: 
object oriented programming matlab object oriented programming principles object oriented programming interview questions object oriented programming java object oriented programming concepts

Buy website traffic cheap

Post a Comment

1 Comments