Junior — Middle
How to distinguish procedural programming from object-oriented programming in terms of their main technical characteristics?
sobes.tech AI
Answer from AI
Procedural programming is based on a sequence of procedure (function) calls that change the program's state. Main characteristics:
- The program consists of functions and data separately.
- Control flow of the program is through sequential execution of functions.
- There is no concept of objects and encapsulation.
Object-oriented programming (OOP) is built around objects that combine data and methods to operate on them. Main characteristics:
- Objects encapsulate state (fields) and behavior (methods).
- Principles of inheritance, polymorphism, and encapsulation are used.
- The program is modeled as an interaction of objects.
Example:
- In procedural style: there is a function
calculateArea(width, height)that takes parameters and returns a result. - In OOP: there is a class
Rectanglewith fieldswidthandheightand a methodgetArea()that returns the area.
// Procedural style
int calculateArea(int width, int height) {
return width * height;
}
// Object-oriented style
class Rectangle {
int width, height;
int getArea() {
return width * height;
}
}