What is the difference between procedural and object oriented programming?

Copied from: Link 1 Link 2 Link 3: 编程中你碰到的哪些问题感觉到面向对象和面向过程的区别? Link 4

1) In C, a data member must be declared GLOBAL inorder to make it accessible by 2 or more functions in the program. It is possible one function may accidentally change values of this global data member. It will be too difficult to debug & identify which function is causing the problem if the program is really big.

The first feature that any programmer would talk about OOP is “data hiding” facility. A class is a feature in OOP which facilitates to pack together different data types along with different functions that manipulate these data members of class. Data members can be declared as private or public inside a class.

that a class is really similar to structure in ‘C’. Just like structure, a class packs together different things into a single entity. The major difference between class and structure is with functions. Structure does not allow to pack together data with functions (structure deals with data only) whereas class allows to pack together data with its associated functions.

A class member can be accessed from external world (outside the class) using an object of the class only!

This feature of data hiding is called as “Data Encapsulation“. Thus one of the major flaws of POP is solved in OOP.

2) Another important feature brought in by OOP is ‘code reusability‘. This is made possible by a feature of classes named “inheritance”. By using inheritance, one class can acquire the properties of another class.

关于方法的调用,Java 中方法的调用,必须是通过某个实例化对象,方法不能独立于类而存在(静态方法除外)。而面向过程的语言,往往可以独立存在。

面向过程注重的是过程,比如吃饭这个函数,具体怎么吃是面向过程的。面向对象是抽象成一个对象,比如一个人拥有吃饭的方法,注重的不是怎么吃,而是人这个对象拥有吃饭这个方法。

从POP和OOP的区别来看,接口是体现其差别最直接的例子。

在POP中,一切任务都可以表示为函数及函数的组合,就是把完成任务所需的过程拼接起来。

而在OOP中,一切任务都化归为类与类之间的合作。在POP中并没有着重强调封装和抽象的概念,但这两个概念在面向对象编程中被频繁提及。它的意思就是,一个类在使用另一个类(或称之为与另一个类合作)时,只关心这个类可以干什么,而不关心这个类究竟怎样完成其任务。这样,凡是具有相同功能的类,其相同处便都可以抽象为接口。

面向过程是一种以“过程”作为中心的编程思想,其中过程的含义就是“完成一件事情的步骤”。将程序划分为不同的阶段,设计好各个阶段如何衔接,然后定义好每个阶段需要处理的数据。 如果说面向过程像一条流水生产线,那么面向对象就像是一个足球队。没有哪个人能够在一场比赛开始的时候,就精确指定每个队员的每一次跑动,每一次出脚,每一次传球……,而只能指定队员的角色(前锋、中场、后卫、门将),然后由队员门自己根据情况做出反应。所以世界上有两个一样的生产线,但绝对不会存在两场一模一样的比赛。