Object-Oriented Programming vs. Procedural Programming
Procedural Programming
In procedural programming (i.e. C), a problem is broken down into subproblems. These are solved separately in modules, so-called functions respectively procedures). A procedure contains a sequence of statements, such as assignments, tests, loops and invokations
of sub procedures with defined input and output.
Procedures process data that has been passed to them via parameter lists as interfaces.
E
xample
Name and a password of an user have to be set. That can be done by call of a function with two parameters n and pwd. The values of parameters are assigned to the variables. Be aware, that direct access to password is possible from everywhere in the programm.
name = "" # global variables
password = ""
def user(n, pwd): # function (procedure) with list of parameters
global name
global password
name = n # values of parameters are assigned to global vars
password = pwd
user("Miller", "Miller's PWD") # call of function "user"
password = "Miller's new PWD" # direct setting of new password is possible
print(password) # direct access is possible
Originally, computers were used almost exclusively to solve tasks whose scope and complexity was relatively limited. The programs were still relatively small (measured by today's standards). For this purpose, the programming concept of procedural programming
was quite sufficient.
However, with increasing computer power, the size and complexity of the programs also grew very much. The computer made a change to a universal work tool. Today the applications extend over areas such as e.g. multimedia, artificial
intelligence and communication.
This requires a greater overview in programming, or more program structure.
This changes required a rethinking of the programming concept as well:
Object-oriented Programming
The real world does not only consist of processes that can be well represented in the abstract procedural way. (Programs always represent reality somehow.)
First of all, the world consists of things, objects, persons, etc., which it has to
consider and thus put into the foreground. These objects have certain properties and interact with each other in a certain way.
Objects use their own methods to process their own data (properties).
If object data (properties)
can only be changed via a method of the object itself and never directly, this is called data encapsulation:
Object
E
xample
The password of an user can only be changed by a "change password" method exactly of that user. It can not be changed directly, which greatly increases program security. The user password of another user is only changeable through the methods "change
password" of this other user.
A password can only be changed by the user himself.
class User: # class definition header -> class classname:
def __init__(self, name, password): # constructor = special method, which is used for creating object
self.__name = name # private attributes "name" and "password" are initialized
self.__password = password # double underscore "__" means private
def changePassword(self, password): # method changePassword sets new value to attribute "password"
self.__password = password
def getPassword(self): # Getter method only get back value to attribute (here from "password")
return self.__password
user1 = User("Miller","Miller's PWD") # creates object "user1" (constructor is invoked implicitly)
user1.changePassword("Miller's new PWD"); # user1's password will be changed by invokation of object's method
print(user1.getPassword()) # get private password only from user1 himself
print(user1.__password) # direct access to private password is not possible
Summary comparison
| Object-oriented Programming |
Procedural Programming |
| + Real world consists of objects, therefore better portability of reality. + Data encapsulation protects the properties of an object. + More structure => more clarity => better maintainability etc. + Reuse of programming code through inheritance is possible. |
+ Slightly less source code for small programs, + Slightly more performance (speed) for interpreter languages |