If you have just begun to start studying Object Oriented Programming (OOP), then you might be wondering what is the difference between object oriented programming and procedural programming? These are two of many types of programming methodologies.
A programming methodology or programming paradigm is a way/style of writing computer programs. For example, in solving a simple quadratic equation, you can choose from three methods of solving quadratic equations namely by factorization, by quadratic formula or by completing the square method. It is up to you what method you use. Similary, you can choose any programming methodology to write a program.
Procedural Programming is an old methodology of writing programs. You break the logic of your program in many functional units. Each functional unit has its own propose and may or may not return any value. The computer executes the statements of the program in the order they were written i.e., from top to bottom. You define your own functions and call them from a main function whenever needed. Upon each call, the function will perform its task specified in its block of code. The most common example of procedural programming language is "C" language. The C language is still used dominantly in various fields around the world. It is more often taught in the educational institutes to introduce the students to basic programming concepts like variables, data types, loops etc.
A disadvantage of procedural programming is related to security. The data could be modified by the code which should not have access to that data. Functions could be accessed by the code that should not have access to the function. For example, write a program that adds and multiplies two numbers. These are two separate tasks that we can define in two separate functions. These two functions would then be called by a main function. But the function that adds two numbers can call the function that multiplies the numbers. This raises concerns on the security of data.
Object Oriented Programming
Object Oriented Programming, on the other hand addresses these concerns by providing you a feature of encapsulation. Other distinguishing characteristics of object oriented programming are inheritance and polymorphism. The encapsulation allows you hide your data and functions or code in a container and restrict access to them from outside. This container is called a Class. A class houses the data and code that manipulates that data. For example, a deep freezer is a container. If the deep freezer is left unguarded then anyone would be able to put anything in it. This can possibly disrupt the functioning of the deep freezer. If you assign a security guard with the deep freezer then only the guard would have access to open the lid of the deep freezer and store things. You can instruct the guard to store only certain things but not everything. The guard would then be called a public method of a class and deep freezer will become a private instance variable. But to store your things you have to build the container that is build the deep freezer. This is called creating an Object of a class.
Another analogy that can help better understand the concepts of object oriented programming is of a car. Before a car can perform any task it has to be built. But before building a car one must have its engineering drawings. From the engineering drawings of a car, one can build a car. The drawings would then be called the class and the car would become the object. From the drawings, many cars could be made so from a class many objects could be made. The functions such as acceleration, transmission of the car are specified in its drawings. Similarly the class has methods, these methods perform the tasks specified by the user. The examples of programming languages that use object oriented programming methodology are C++ and JAVA.
Functions in C language are called methods in JAVA. Methods perform in the same way as functions. They return values, receive values (parameters) and have a name. Like a main function in C, there is also a main method in JAVA which is automatically called when the program starts executing.