473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 41494
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

"enchantingdb" <ro*****@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.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 "architecture 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
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...
2
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...
15
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...
18
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...
25
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...
23
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
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...
2
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...
3
by: TheeStudent | last post by:
Can someone please make recommendations on how to best exploit polymorphism, inheritance and encapsulation in C Sharp.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.