You asked specifically about 2 things: Inheritance and Interfaces. Let's
start with Inheritance, as that is the easiest to understand.
In Object-oriented programming, you have classes that encapsulate
functionality, rather than functions existing outside of any class. In
procedural programming, you have what is called function libraries, for
re-usable functions. Function libraries are DLLs, but they contain only
functions, not classes. In OOP, you have classes that encapsulate
functionality. For re-usability, you can use inheritance. For example, let's
talk about System.Web.UI.WebControl. All WebControls inherit this class.
Why? Because they all have some things in common. By creating a base class
that contains all the functionality that is shared by these classes, you
re-use the base class and its functionality in each derived class. For
example, all WebControls have an Attributes property, because all HTML
objects have attributes. By encapsulating this in a base class, you don't
need to re-write the code for each derived class, and if there is a need to
change this functionality, you change it in one place (the base class) and
all derived classes inherit that functionality.
Interfaces are a bit different. They define a "contract" if you will, that
specifies certain characteristics that are required by any class that
implements the Interface. These characteristics are either fields,
properties, or methods, which must have a certain signature. For example,
when creating a Server Control that implements IPostBackEventer, you must
implement a method called "RaisePostBackEvent." Why? Because, in order for
the class to handle PostBack events, it must have a method which can be
called from outside the class, which conforms to the signature of the
RaisePostBack method in the IPostBackEventHandler contract. In other words,
there is an external dependency in the architecture, which expects to find a
method with that signature.
Does that help?
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Simon" <sh856531@microsofts_free_email_service.com> wrote in message
news:#4**************@TK2MSFTNGP09.phx.gbl...
Hi all,
I'm hoping that some of you clever chaps could offer me some advice on
code reuse.
You see, whenever I make applications, I typically only find very limited
scope for code reuse. I also only find limited use for inheritance. For
example, the various types of users that my system might have to deal
with.
I'm wondering if anyone could give me some tips on how to identify areas
of my design that could be reusable, where I can use inheritance and where to
use interfaces. I've read all the books that talk about why these things
are important but very few of them show you how to do it in the real world.
If anyone could point me to some online resources on this then that would
be great. I could also do with a couple of books on how to spot reusable
aspects of my designs and so on.
Any help would be great
Thanks all
Simon