473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disadvantages of Polymorphism, Inheritance and Encapsulation

I have an exam tomorrow that covers the perceived advantages and
disadvantages of object oriented programming, in particular
polymorphism, inheritance and encapsulation. I know the advantages but
am not clear on the disadvantages. I have had a look on the Web and in
newsgroups but couldn't find much. As time is running out, I thought I
would post here and hope that someone would reply.

Thanks

Rob

-------------------------------------------
A Welsh View - http://xo.typepad.com
Technology, games, weird stories & links...

Nov 16 '05 #1
3 41519
OO is such a tremendous improvement in application design over previous
functional programming models that it really is difficult to come up with
any deep detractions. I think you could say something like:

1. It's esoteric. Not very easy for the beginner to just pick up and go with
it. Rather it takes often years of dedication before abstraction becomes
second nature.

2. Huge up-front hit in migrating legacy code as it often has to re-designed
from scratch. Of course in the long run, in terms of re-use, performance and
robustness, this eventually becomes an advantage.

3. Whenever you use "high-level" languages, some degree of low-level control
is forfieted. Ususally in frameworks which are designed to interop with
legacy APIs this isn't a problem but think of the performance hit you get
when you compare C# with pure C. Of course, most would willingly sacrifice a
10% performance hit, versus a 200% increase in productivity any day.

ok,
aq

"enchanting db" <ro*****@gmail. com> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...
I have an exam tomorrow that covers the perceived advantages and
disadvantages of object oriented programming, in particular
polymorphism, inheritance and encapsulation. I know the advantages but
am not clear on the disadvantages. I have had a look on the Web and in
newsgroups but couldn't find much. As time is running out, I thought I
would post here and hope that someone would reply.

Thanks

Rob

-------------------------------------------
A Welsh View - http://xo.typepad.com
Technology, games, weird stories & links...

Nov 16 '05 #2
In addition to the disadvantages mentioned by Ahmed, here are a few
more.

1. O-O programming requries more organization and greater effort spent
on designing the architecture up front, at least if you want to avoid
massive code churn (rewriting). In a third-generation language like C,
if you get the design wrong, you can usually fudge the code and mash
things around to get them to work out. In C# or C++, if you screw up
the class hierarchy, you are truly screwed.

This may sound like you just have to take more time up front for a
later payoff, but remember that "architectu re is a risk" (read "The Big
Ball of Mud" by Foote and Yoder: http://www.laputan.org/mud/mud.html ).
Sometimes you don't know enough about the problem, or you don't know
the language well enough to produce a good architectural design up
front. What do you do then? Well, you take a stab at it, then discover
later on that you were wrong, and that as a result you have to
practically start from scratch, because in O-O the really important
decisions are made very early in the development cycle (during analysis
/ planning rather than during coding).

2. Inheritance can make code harder to read. When you have classes
inheriting from classes inheriting from classes and interfaces, you can
end up threading through code looking for which method is _really_
being called. In C, you can get away with using a simple editor and a
command-line compiler. In C# you have Intellisense, which is not just a
nice feature--you _need_ it, because without Intellisense and automated
documentation you really have _no_ idea what's going on inside a class.

3. Inheritance and polymorphism mean that you have to understand the
design, the programmer's original intent, very well before you start to
maintain the code. In a 3GL world it was important to understand the
original programmer's mindset to some degree, but I managed to work on
very badly written programs by biting off small chunks and rewriting
them. In C#, the code isn't nearly as important as the overall design
of the class hiearchy, which requires maintenance programmers to do
more studying of the code before they get up to speed.

Again, when you have classes inheriting from classes inheriting from
interfaces, you had better understand why all of that was built the way
it was, or making a small change "here" will ripple throughout the
entire class hierarchy and cause changes in behaviour in the least
expected places.

4. I'm trying to think of a disadvantage of encapsulation, and I'm
having trouble, given that I did it in C for 15 years before I even
touched an O-O language. It does require a lot more code, it's true: I
find that programs that employ proper encapsulation are maybe 30%
bigger than ones that don't. However, they _work_, and they're _cheap_
to maintain, so I see the tradeoff as worthwhile every time.

Of course, in most jobs if you want to look like a star you just toss
encapsulation and write crappy, quick-and-dirty code to get the job
done. You come in ahead of schedule and under budget, your boss thinks
you're a genius, and you dump the whole ugly mess on the maintenance
team and move on to your next highly visible project. So long as you
can keep one step ahead of the ticking time bombs you're creating in
the process, and blame the huge number of latent bugs on the
maintenance team (because by now you're on an unrelated project),
you're laughing. Of course, this doesn't work at all if you're on a
small team that maintains what they build, which is why I could never
bring myself to do it. I know more than a few guys who spun themselves
into top consulting jobs / management positions doing that, though. So,
I guess one disadvantage of encapsulation is that although it's the
"right" way to do things, some people who ignore it will get the corner
office while you're still toiling in the programming team. :)

5. Encapsulation hides details about how a class implements its
functionality, which is good from a maintenance and understanding
standpoint, but also makes it easy to write programs that waddle into
memory like enormous, bloated sows, and sit down with a thump, sending
all of your other applications scattering for the swap file. Because
you don't directly know what an object needs in order to do its job,
you don't really know what are the consequences of making 10,000 of the
things. Try sucking a whole lot of data from a database into a DataSet
object in ADO.NET to see what I mean. Ouch.

This isn't necessarily a bad thing, but again it's a learning curve,
and a lot of programmers don't bother learning: they just build fat
piggish programs that use up 1GB of memory and tell their bosses to
shell out for more RAM. What I mean is that if you really need 1GB of
memory, then you need it. In the 3GL days I wrote one or two programs
that used that kind of memory, but only one or two, and only for
specific reasons. In the O-O world it's just too easy (because of
encapsulation) to write code like that; it takes training and
experience to write lean applications.

5. The same goes for CPU cycles: because encapsulation hides how a
class does its work, it also hides how much code has to run in order to
do that work.

6. Inheritance and polymorphism bring with them a strong temptation to
overgeneralize everything, "because you can." It's far easier for
programmers to go nuts and try to design some new class that is vast
overkill for the problem they're trying to solve. The problem here is
one of efficiency: frequently these "do anything" classes are at the
top of a large pyramid of other supporting classes that together make a
huge, ungainly monster when in fact a simple little tool would have
sufficed. Here we're talking about the guy who builds a whole new
formatting language in order to print strings to a log file, rather
than just using String.Format() . It's just easier to do in O-O, and
easier to do inefficiently.

Nov 16 '05 #3
Grab a copy of Effective Java by Bloch and read "Favor composition over
[implementation] inheritance" "Used inappropriately it leads to fragil
software." "Unlike method invocation, inheritance breaks encapsulation."
The
superclass may change and the subclass may break even if its code was
not
touched. for instance the superclass may add a new method that allows an
illegal insert into a collection. Inheritance propogates any flaws in
the API to
your new superclass. you can correct a flaw using composition etc. etc
etc.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
2804
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic...
2
1815
by: Tony Johansson | last post by:
Hello experts! My question is what advantage and disadvantage have public inheritance. The answer that I have to this is that sometimes it is an advantage to let a client have access to everything declared in the public section of the derived class and the base class and sometimed it is an disadvantage. Another important advantage is...
15
330
by: scorpion53061 | last post by:
I am just now really starting to get into inheritance - controls, datasets, views all that stuff and I am seeing the enormous savings in performance and even watching the exe file size go down which is making me very happy. I am frustrated a bit though it seems to be costing me more time in that I am kind of not seeing the entire picture in...
18
3843
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two different objects to respond to the same request message in their own unique way" I thought that it was: "the ability of same object to...
25
3006
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am aware of that fact that if you have a class B, that inherits from A, then List<Bdoes NOT inherit from List<A>. So I understand why the example below...
23
4586
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
1
10069
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which you may have seen many times in one form or another. An analysis of this example produces several problems that are not obvious but which will...
2
2391
by: gas | last post by:
Hi So, I have the following problem: I have to store several types of Cells, (call them A, B, ...), where the number of Cell types is going to increase as we continue development. Among other things, I have to compute inductances between all combinations of these cells. How the inductance is computed depends entirely on the type of...
3
1946
by: TheeStudent | last post by:
Can someone please make recommendations on how to best exploit polymorphism, inheritance and encapsulation in C Sharp.
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.