Object-Oriented – Relation Between Object Orientation and Algorithms

algorithmsobject-oriented

As I read some algorithms textbooks, they are full of clever procedures for some problems (sorting, shortest path) or some general methods (recursive algorithms, divide and conquer, dynamic programming…). I found few traces of object oriented programming there; (Why they are more procedure-oriented?).

Then I was thinking:

  • What is the relation between algorithms and OOP? Are they two independent topics?
  • Are there some problems which can only be presented and solved by OOP?
  • How can OOP help algorithms? Or in which direction it can affect it?

Best Answer

First, lets define what we mean by OOP. By OOP I mean primarily :

  • Encapsulation and hiding of details in class.
  • Polymorphism of behavior through inheritance and virtual methods.

Now, to answer your question:

What is the relation between algorithms and OOP? Are they two independent topics?

Yes.

Are there some problems which only can be presented and solved by OOP?

No. OOP primary offers convenience and ability to reason about code for programmer. It doesn't increase your expressive power.

How OOP can help algorithms? Or in which direction it can affect it?

Like I said above. Both points I described OOP as apply here. Being able to hide details of algorithms and their data structures can help reason about the whole thing. Many algorithms contain details you don't want user of that algorithm messing around with. Hiding those details helps a lot.

Ability to have polymorphic behavior is great too. List is defined as being able to add/remove/clear the items anywhere in the collection. But it can be implemented as resizable array, double-linked or single-linked, etc. Having single API for multiple implementations can help reuse.

Why algorithms textbook are more procedure-oriented?

Like I said, OOP is not necessary to implement an algorithm. Also, many algorithms are old and created when OOP was still not widespread. So it might also be a historical thing.