Reputation: 710
Does OOP just organizes code (and make your life easier when maintaining, updating application) or it organizes and OPTIMIZES (perfomance, under the hood)?
For example is:
public class Ferrari {
..speed;
..color;
..changeGear(){..change-gear-class-releated-stuff like 8 gears or something}
..turnLeft(){...}
the same like:
public class Ferrari extends Car {
@Override
..changeGear(){
super.changerGear();
..change-gear-class-releated-stuff like 8 gears or something
}
public class Car{
..speed;
..color;
..changeGear(){...}
..turnLeft(){...}
for the sake of perfomance?
Upvotes: 4
Views: 364
Reputation: 21
In compiled languages it does not improve performances, probably just increase compile time.
Upvotes: 2
Reputation: 118691
Basically, no.
As with everything, you have to stick a big IT DEPENDS on just about everything, but as far as execution performance, no, inheritance does not improve performance.
What it can improve is overall code density, as the shared code is truly shared among the subclasses, rather than duplicated.
In some language implementation, inheritance can slow performance. Consider the runtime searching for a method on a subclass. When it first looks for the method, it finds that the method does not exist on the subclass and must then perform a search on the respective super classes of the method. Those super class searches can certainly have some impact.
But that's an implementation detail. Other systems will pay that cost at compile time, rather than runtime, thus having no actual performance cost.
So, you can see that it's not necessarily a black and white solution.
Upvotes: 2
Reputation: 8246
Object orientation may prevent certain algorithmic optimizations, because of inheritance. Two algorithms may work particularly well together, but if they are hidden behind OO interfaces, the possibility to use their synergy is lost.
Look at numerical libraries. A lot of them (not only those written in the 60s or 70s) are not OOP. There is a reason for that -- numerical algorithms work better as a set of decoupled modules than as OO hierarchies with interfaces and encapsulation.
Upvotes: 1