Object-Oriented Programming- concepts and problem solving.

Riyan Pahuja
15 min readJan 13, 2021

--

Object Oriented programming

Abstract

The world consists of objects. Each object has its own behaviour and characteristics. Imagine how hard it would be to build an aeroplane if we had to design everything from scratch, including reinventing the wheel, vulcanizing rubber and hardening plastic etc. Fortunately, we have stored the information common to all air crafts previously and simply use it which saves a huge amount of time and reduces the complexity. This is exactly the concept on which the object-oriented paradigm is built. Common characteristics between objects is stored in a class and then objects are built as their instances.

Problem Solving

Computers were designed to solve problems that human would usually take a lot of time to solve. Hence, this makes problem solving a core domain of computer science and what a programmer must learn. We can call computer programmers as problem solvers and in order to solve a problem they must:

1. Represent and understand the data(information) that describes the problem.

2. Map the steps to use the information to solve the problem.

In computer science, problem solving has the following 6 steps,

1. Understanding the problem

2. Formulating a model

3. Developing an algorithm

4. Writing a subsequent program

5. Testing and improving the program

6. Evaluating the solution.

For example:

Write a program to calculate the average of the grades of a student.

1. Understanding the problem:

The question has asked to find the mean of all the given grades of a student

2. Formulating a model:

Average = grade1 + grade2 + …. + gradeN / number of records

3. Developing an algorithm:

grades_array = grades, sum=0

for grade in grade array

add grades -> sum

average <- sum / length(grades_array)

4. Writing a subsequent program

A program is written following the above algorithm in the programming language of choice.

5. Testing and improving the program

The program is tested against all kinds of data including but not limited to corner cases that may break the algorithm such as the length of the array being returned as 0.

6. Evaluating the solution

The solution is evaluated and checked whether the output is correct or not.

Algorithms

When we are asked to perform a task, we require a set of instructions to perform that task. These instructions can be previously stored in the memory or explicitly stated at the time of task assignment. An algorithm is just a fancy name for a set of instructions.

Programmers use algorithms to tell a machine how to perform a certain task. For example: If a human is asked to get a stack of papers off the table, the instructions given would be, go to the table, turn towards the stack of papers, pick up the papers. Similarly, when a task is assigned to a computer/machine we need to provide it a set of instruction or algorithm in short to achieve a successful result in the said task.

A good programmer can define the necessary steps to give to a computer for completion of a task. However, a computer is bound by the limited set of possible steps. For example, given a set of number, it can add them. But if it is required to calculate the mean of the given set, the task is beyond its capability. A programmer would need to specify the steps for it to achieve this,

1. Add the numbers and save the result in a variable.

2. Calculate the length of the set and store it in the variable

3. Divide the sum variable by the length variable and store the result in a third variable.

4. Convey the result.

Note: Step 2 requires another set of instructions to be completed and is not within the capability of a computer.

Object Oriented Programming

A brief history

The first ever object-oriented language invented was named Simula. Ole-Johan dal and Nygaard laid the foundation stone of object-oriented programming that is now the dominating version of programming in the modern day. Through their design Simula 1 was born. Simula 1 along with Simula 67 was awarded the IEEE von Neuman medal in 2002. Simula 1 programming language had neither classes nor inheritance, however Simula 67 introduced the idea of the “fundamentals of the object-oriented programming”.

Samlltalk-72, which was an early version of Smalltalk, refined the idea of objects as little computers. Tim Rentsch in his paper named “object-oriented programming”, published in 1982, said

“The immediate ancestor of object-oriented programming is the programming language Simula. The Smalltalk programming language system carried the object-oriented paradigm to a smoother model.” (Rentsch)

What is Object Oriented Programming?

Object Oriented programming, which may be thought as a new concept in the programming world by many was developed in the 1960s along with most of its concepts used in the present.

Object Oriented programming breaks down a program into various tasks which are then assigned to different objects. Considering a real-world example, Mike has been some experiencing some issues with his computer which he cannot fix on his own. He would need the help of an engineer provided by the manufacturing company. Mikes contacts the technical support and requests for an engineer to be sent to his home for fixing the issues. The technical support then does the required for assigning an engineer to this task. The engineer arrives at Mike’ s house and fixes the computer and takes his leave. In this example Mike uses an object/agent (technical support) to pass the message to the engineer. It is important to take note on how Mike does not know or need to know how the technical support contacted the engineer and assigned him the task. Then the engineer then does the assigned task and takes his leave. We must note again that Mike does not know how the computer was fixed by the engineer. This is concept is referred to as abstraction.

The concept of object-oriented programming dictates building reusable code and having the willingness of using code built by other. Technical support is a code built by someone and was used by Mike and can also be used by others for similar purposes.

Concepts of Object-Oriented Programming.

1. Method and Methods

In the example presented in the previous section Mike passes a message to technical support for calling upon a function. Similarly, in the OOP we pass messages to objects to call upon methods that would do a specific task for us. A task(method) is called upon when we pass a message to an object with some additional information called arguments. A method is only called by an object if the method has the same name as the message passed.

l. Message Vs Procedure Calls

Both consist of well-defined steps of action to perform a task after a request has been made. However, A message requires a receiver(object) to call upon a method to perform a task. A procedure call requires none such receiver. The method to call upon for the task is at the sole discretion of the receiver(object).

Mike could have asked his friend to fix his computer and could have gotten a satisfactory outcome. But the method used by this friend would have been different than the one used by the technical support. Mike could have also asked a plumber to fix his computer, but the plumber might not have the appropriate method to perform the task and would put forward an appropriate error message.

In context of computers, the method that is called upon depends upon the receiver. Different receiver may call upon different method to solve the same task. The presence of this receiver that acts as a middleman is what differentiates message passing from procedure calls.

2. Classes and Objects.

Objects: Objects act as receivers. They receive messages from the program and use them to call upon method for performing tasks. Objects contain not only methods but also data. Objects provide security to the data by not disclosing any data marked as private to the user. In the example, the technical support does not reveal the internal working to Mike, they simply receive the request and perform the task. In this scenario Mike does not know what data is used for the completion of the request. Similarly, in OOP the object does not let the user know what data it contains and how it is using it.

Classes: Classes act as a blueprint for an object. They define the behaviour and inner working of the object. All the objects derived from a class must follow the same behaviour. Technical support operator that talked to Mike in the above example was an object that followed the behaviour that was defined by a class which may be called technical support staff. All the other technical support staff communicating with a customer would also follow the same behaviour defined by the class.

Example code:

Code for the tech support example
Tech support example explaining classes and objects

In the above given code TechSupport is the class that dictates the behaviour of any object that is based on this class. Operator1 is the object that follows the design that has been laid down by the class TechSupport. A message is passed to the object which then receives the message and matches it with a corresponding method which is thereafter called and performs a task.

Objects can also be created within the same class.

Sample code:

Tech support example for objects within a class
Tech support example for objects within a class

In the above code the object is defined within the same class and then carries out the task successfully.

3. Inheritance

A lot of objects. in the real world, contain a lot of similarities and may follow the same behaviour. Let us consider a class Human. Now a man and a woman are two objects that are both part of the class Human. They both have various similar characteristic and behaviour. They both consume food, talk, see, hear etc. However, they both also contain some dissimilarities, like body structure, reproductive organs etc. Now due to these differences we cannot have man and women be instances of the same class. So, one way to approach this would be to create two different classes for man and women. But then there would also be a lot of repetitive code that for the two classes due to the similarities. So, to solve this we could define a super class called Human that would contain the code for the similar characteristics and behaviour of the two classes and then derive these two classes from the super class (parent class). The sub classes (child classes) would contain the code from the super class and would have the ability to override it if need be or add some of its own code. This ability of OOP is called inheritance.

Inheritance in OOP

The above code demonstrates how inheritance works. The class Man and Woman is derived from the super class Human. Both Man and Woman contain the method sleep() of the Human class and are able to call upon it. They also redefine the method shave according to their own needs. This is called method overriding.

4. Polymorphism

Polymorphism is one of the most important characteristics of an object-oriented programming language. It refers to one object taking many forms i.e., using one thing for performing different tasks.

Method overriding: In the above example we saw two derived classes using the same function shave() to perform two different tasks. This is called run time polymorphism or method overriding.

Method Overloading: When we define more than one method with the same name in the same class to different method, we call it method overriding. An object-oriented programming language differentiates between the two method by the number or type of parameters passed in the method. If the number of parameters does not differ an error message will be generated. Let us take an example: Mary and Ajay were asked to cook a meal for the guests that were arriving for dinner. Mary cooked a wonderful dish of Chicken Curry whereas Ajay cooked a delicious Pasta. Mary and Ajay cooked quite different things and followed different instructions. However, the message passed to both was the same that is “cook a meal”. Similarly, when the message that needs to be passed to an object is similar, but the task needs to be carried out a differently bases on the specifications of the task we use method overloading.

Example code:

Method overloading

As it is evident from the code the same name for two different methods is used. The two methods print 2 different things however they differ by the type of the parameters passed to the method.

Virtual Functions: A virtual function allows the base class to override a function defined in the parent class. If a function in base class is not defined as virtual then the base class will not override that function. In programming languages such as C++, to achieve rum time polymorphism that is method overriding the parent class method must be defined as virtual. However, in programming language such as Java, all the method are defined as virtual by default.

Abstract classes: An abstract class acts as super class from which more specific classes can be derived. It defines general methods and forces the sub classes to override them. For example, all cars contain a drive function, a neutral function and a reverse function. But how they implement it can be quite different among different brands and different models. A lower model of Maruti car may have a shift drive model and the upper model of the same car may have an automatic drive model. In OOP we can define Maruti as an abstract class that forces all it is sub classes that would the car models to have a drive function in it. In languages such as Java the method should be declared abstract whereas languages such as C++ require the function to be declared virtual. A function that is declared as virtual and as not body is known as pure virtual function. The abstract method of Java can be considered as a pure virtual function.

Sample code:

A code explaining abstract classes

Note: An abstract class can contain both abstract and non-abstract methods. However, it will throw an error if an abstract method is not overridden in the sub class.

5. Abstraction & Encapsulation:

Abstraction is a concept of dealing with the action without worrying about the details of the process of the action. Abstraction has been discussed before in this paper however let us reiterate. Considering a previous example, where Mike contacted the technical support for assistance with his computer. Mike is not informed about how the engineer was contacted, how the engineer reached to Mike’s home and how the computer was fixed. Mike was spared from the details of the process. Similarly, in the world of OOP the objects receive the message, call the method and the method does its job and user does not know how the action was performed by the method.

Objects consist of both data and methods. The data can only be accessed and modified by the object’s methods. This bundling of data and methods together is called encapsulation. Data encapsulation provides added security to the data and prevents it from being modified by anything other than the methods of the object to which that belongs. In the fixing the computer example, the technical support operator must have data, such as customer phone number, employee phone number, customer address etc., encapsulated with the functions they can perform that were used together to contact the engineer and get him/her to Mike’s house.

Advantages of Object-Oriented Programming

Object Oriented Programming has been around for decades now and has gone through evolution to becomes what it is in the modern day. But the inherent concepts such as objects, classes, abstraction, encapsulation etc remain unchanged. We should also keep in mind that different programming languages may have their own implementation of the OOPs concepts.

Here are a few advantages of object-oriented programming without dependence on any specific language.

1. The real world is made up of objects and each object has its own behaviour and characteristics. Using object-oriented programming we can represent real world problems and solve them in a much more efficient manner. This paper has represented several real-world problems and how it is solved using objects and classes.

2. Object oriented programming provides higher data security as it bundles data and methods together and only allows the data to be accessed and modified by the methods. It also only gives the user necessary information and hides all the unnecessary details making it more user friendly. Recall in how technical support officer did not trouble Mike with ant of the unnecessary information about how the engineer will get there and do his job.

3. In object-oriented programming we can write reusable code using inheritance which not only makes our code more concise and requires less space but also makes it much more readable. Recall how we were able to use the same method that was written in the Human class for both its sub classes, Man and Woman.

4. As codes is modular it is easier to debug. As all objects are independent i.e., they do not dependent on any of the other objects for execution we can quickly and very easily figure out where the problem in our code is. If the engineer has not arrived at Mike’s house it would have been easy for company to figure out that the problem must have been in the technical support class.

5. Using polymorphism, we can override or overload a method to make it perform more than one task. Polymorphism gives the code the super ability to shape shift into something else. Recall how the ‘shave’ method was made to do different things depending upon which object called it and how Mary and Ajay were able to cook different things using the ‘cook’ method.

6. Object oriented programming language require less space to store the code as most of the objects are created at run time when they are called and are destroyed after they have successfully accomplished their purpose.

Popularity of Object-Oriented Programming and Its Problems

Object oriented programming is a fairly popular concept. And it had become even popular now that it has been followed up by object-oriented analysis and object-oriented design. However, this concept can be hard to understand for new programmers or programmers who have usually coded in functional programming paradigm or procedural programming languages only. This causes the programming world to be split in two sections, the one who has adopted object-oriented programming and one who hates it. It is not only new programmers who are against the idea of object-oriented programming but some experienced programmers such as, Ilya Suzdalnitski, who is a senior full stack developer at Replicon has called OOP a “Trillion-dollar disaster” (Suzdalnitski, 2019). People believe that object-oriented programming is a paradigm for elite programmers whereas the code base is mostly edited by average programmers since they are more in numbers. This requirement of elite programmers, who are harder keep around, may cause the paradigm to fail over time.

However, no matter how much object-oriented programming is hated by experienced or novice programmers the success of object programming languages such as Java and C++ are undeniable.

Even previously functional programming(scripting) languages such as JavaScript have now added classes in their ES6 update. Also, the major dependency of app development, web development and other development paradigms on object-oriented programming languages such as Java, Kotlin, Python and not to mention the promotion of these languages by big dominating companies such as Google and Facebook will be stopping the object-oriented programming paradigm from dying out.

Conclusion

Object oriented programming has gained so much popularity that it does not seem to stop. Most of the problems in real life are being solved using this paradigm. Even though, object-oriented programming may not be very well understood by everyone it is an undeniable fact that it has made revolutionary changes in the programming world. This paper tries to explain each terminology and concept in relation to their real-life usage which proves its importance in the real-world problem solving. It is essential to understand how the paradigm works and effect the problem before selecting it to reach the solution.

References

1. https://docs.oracle.com/javase/tutorial/java/concepts/index.html

2.Object-oriented programming: Some history, and challenges for the next fifty years. Andrew P. Black, Portland State University.

3.Object-oriented programming and its concepts. Ashwin Urdhwareshe, Department of Technology Management, University of Bridgeport.

4. Introduction to Network Simulator NS2 (second edition). Teerawat Issariyakul Ekram Hossain

5.Object-Oriented Programming. Tim Rentsch, Computer Science Department, University of Southern California.

6.Data abstraction, data encapsulation and object-oriented programming. A. Toni Cohen, Department of Computer and Information Sciences, University of Delaware.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Riyan Pahuja
Riyan Pahuja

Written by Riyan Pahuja

Studying engineering as information technology major.

No responses yet

Write a response