Click here to Skip to main content
15,884,088 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello,

can anyone please tell me which oops concept offer maximum code reusability as I'm not sure among Encapsulation, Inheritence, Overriding and Overloading......?



Thanks in Advance,
Gobind
Posted
Comments
jsolutions_uk 25-Sep-14 9:12am    
Interesting discussion over here.

Code reuse via an inheritance hierarchy is a sure fire way to get in a right old mess with tightly coupled dependencies. Personally I try to limit my use of class hierarchies to where I really need run time polymorphism.

Occasionally this results in some abstract base classes containing virtual methods that can be overridden / called by derived classes, but this is usually a result of needing the polymorphism and I should probably refactor the code to encapsulate the "reusable" state/behaviour outside the base class and have a more loosely coupled dependency :)

On inheritance: here and here.

Code reuse is a general target of OOP and (theoretically) all the three pillars of OOP, namely Encapsulation, Inheritance and Polymorphism support it. Practically code reuse is almost a myth: in order to be reusable a piece of software must solve a rather general problem and be very careful coded (requiremts fulfilled, for instance, by a design pattern).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Sep-14 13:32pm    
Good point, but I voted 4.
First of all, OOP way of code reuse is not the myth at all; the problem is different.

The real problems are: 1) this way has a problem: the reuse really takes place only if the future possible paths of all the derived classes are reasonably well predicted; unpredictable features added in inheritance is are also quite possible, but more problematic; 2) most developers lack the ability of such a wise vision requires to utilize the reuse as described in #1. (The second statements might be considered as arrogant, but what can I do: this is really my opinion, in particular, based on my CodeProject experience; by the way, I did not state that I'm wise enough myself.)

Also, there are intrinsic architectural problems associated to both multiple inheritance and lack of it. Interestingly, I have a friend who put forward the generalization of OOP which overcome that problem. The idea is: the decision of having virtual vs non-virtual base class becomes the subject of late binding itself. His design had a cost of another level of indirection: instead of VMT, two different tables. Not a great cost for a break-through (sorry, I have no references).

Another problem is the answer: OP needs to be explained that there are no any different "OOP concepts" to chose from (or something like that, implied by the question). In fact, there is one holistic OOP concept (with two slightly separated areas: multiple inheritance and interfaces, each of them can be missing), and all listed concept support each other to result in the central one: late binding. Without that one, there is no "real" OOP at all, no matter how well the other concepts are implemented.

—SA
Sergey Alexandrovich Kryukov 24-Sep-14 13:50pm    
So, I added the answer addressing the last issue. Please see Solution 2.
—SA
My answer, in addition to Solution 1, is mainly in my comment to that solution.

I would like to reiterate: the question is not really adequate in the following sense: it apparently assumes that OOP concepts can be chosen in one or another combination, for best results. This is not really the case. For example, you can build some design on the combination of generics (non-OOP concept also available in some non-OOP languages, such as first version of Ada) and OOP. Then you really have choice.

Inside OOP there is only one choice: to use OOP or not (again, not mentioning interfaces, multiple inheritance and also delegates, which really gives the choice; let's assume you have OOP without those feature, just for the sake of discussion). Not using OOP using a pure OOP language may mean different approaches, but is possible when one uses classes formally, not leveraging OOP power in any way. How? One can use only static methods, never virtual ones, not even instance non-virtual methods (in some languages, static methods can be abstract/virtual, quite naturally). That would be 100% equivalent to having old "structured language" without a glimpse of OOP. Even if one uses inheritance, instance methods, it does not make the use of the OOP technology. And finally, some especially silly "developers" even use the keyword "virtual", but not really leverage "virtuality". Such programs could be logically equivalent to the same programs with the words "virtual" removed. I really saw such things. :-(

The technology uses OOP when late binding is really used and leveraged. Alternative approach using interfaces, with or without abstract/virtual/override, can also be considered OOP. But other concepts are not the subject of choice. If OOP power is really leveraged, all other "concepts" are not optional ones, they are needed to support the central ones. There is no any late binding and/or polymorphism without, say, encapsulation and inheritance — otherwise there would be nothing to "bind".

(By the way, not many people who actually can leverage OOP power understand the possibility of polymorphism based on interfaces and not base class. Frankly, I got it much later myself.)

These considerations, perhaps, illustrate the weakness of OOP pedagogic in general. The traditional approach tends to tread the mentioned "concepts" as "equal", which creates some illusions in those who did not grasp the main idea. As I can observe, such people even manage to graduate and start working, having no idea how OOP works, but able to tell us "correct words" about those concepts. This is pretty sad.

[EDIT]

I forgot to address one thing. "Overriding" is really an OOP concept. So called "overloading" is not. The second concept does not deserve a separate name itself. This is one of the worst and confusing term which misleads too many people in whole industry.

The "term" is very confusing: it hardly could be called "overloading", because nothing is "loaded". It merely means that the syntax allows completely different formally unrelated methods to have the same name. Pretty trivial thing. A compiler can distinguish them by profile inferred from the call code. Or not; then the compilation error is issued. OOP is not needed to have this trivial feature.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900