473,785 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

interface vs. inheritance

Are there any good references/articles/books which provide clarity toward my
insecurity still on deciding how to model a complex system? I still feel
uncomfortable with my understanding, even though I have worked with these
systems on when to decide to use interfaces (and how they should be
developed) as opposed to or complemented by the use of inheritance from base
classes.
If I am thinking from the point of view of some specific activity and I
begin to think at the method level, how do I think about its implementation
at a code syntax level as to whether that method is obtained through
inheritance from a base class, or if I obtain it via an interface which I
have also inherited from?
Thank you. -hazz
Nov 16 '05 #1
7 12877
I tend to follow this guideline. If the relationship is clearly "is-a",
I use inheritance. If it is more like "can-be", I use interfaces. For
eg, TextBox "is-a" Control, ArrayList "can-be" enumerated (so it
implements IEnumerable). In general, if an object can be treated in
different ways (can be enumerated, can be cloned..), I implement
interfaces.

And obviously, if you're going to use existing functionality AND extend
it, I derive from it.

Regards
Senthil

Nov 16 '05 #2
Thank you so much for that very simple explanation Senthil. I think I get
that. I have to think a bit more about the "can-be" relationships.

Also as you stated, that if you are merely extending existing functionality,
you derive from it.
By that I assume you mean the other role of interfaces? ie. as a contract
with the underlying classes which they represent whose purpose is to provide
the freedom to change the underlying code without having to change the
interface ??? Wait a minute. In a project build I still have to rebuild the
whole solution with different projects whether they contain interfaces with
their associated classes or just the classes without any interface to
represent them, right? If the code changes in a class and I want to use it
somewhere outside of that namespace where it is referred to, I have to
rebuild the whole project so that Visual Studio (and my consumer class) will
recognize the new functionality.

Now I am getting into the reference dilemma which I puzzle over a bit on
complex projects ie. circular references, build problems where sometimes a
solution with many projects needs to be decomposed into smaller builds to
build without error.

If I create a project/namespace with a certain scope, like "datalayer" and
another, "applicatio n specific support utilities," then why would I use
interfaces to represent the classes contained in these projects? If I extend
the functionality of the application using either of these two different
examples, couldn't I equally inherit the interface by the 'class :
interface' syntax or by the 'using' statement? Both require I set a
reference prior to using them and both require I rebuild after any changes
are made to any of the participant classes. What I am getting at is that I
don't see how interfaces add any advantage here for extending functionality?
Is it because I can inherit from multiple interfaces? I still don't buy that
because I could just add one more 'using' statement and grab another
namespace with its underlying classes and not have to mess at all with the
time it takes to create an interface.

thanks for any clarification if I haven't absolutely lost you. ;-) It is o.k
to say my question makes absolutely no sense or I am dead wrong about my
understanding. -hazz
"sadhu" <se**********@w devs.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
I tend to follow this guideline. If the relationship is clearly "is-a",
I use inheritance. If it is more like "can-be", I use interfaces. For
eg, TextBox "is-a" Control, ArrayList "can-be" enumerated (so it
implements IEnumerable). In general, if an object can be treated in
different ways (can be enumerated, can be cloned..), I implement
interfaces.

And obviously, if you're going to use existing functionality AND extend
it, I derive from it.

Regards
Senthil

Nov 16 '05 #3
1)Favor containment.
2)Avoid extending a class unless it was designed to be extended.
3)If you have a well defined contract that will be implemented by many
classes use an interface. An interface represents a purely abstract IS_A
relationship.
4)If the contract is evolving, consider using a base class so that you
can provide default implementations of any new methods so that clients
that use your base class can be recompiled without breaking.
5)Use a base class if you need to include implementation details.

That said... every time I try to solve a problem I seem to start by
describing the contract with an interface say IVersionable. Then I end
up writing an abstract class with default implementations as in
AbstractVersion . Then I end up creating concrete classes based on the
Abstract class as in Version : AbstractVersion .

So I would try this pattern: Interface -- AbstractInterfa ce -- Class
which uses both interfaces and base classes and offers both options to
the user of the "interface/class."

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Thank you Jeff. I am digesting this. It's a big meal. As my Dad used to say,
my eyes are bigger than my stomach.

Just a bit of qualification of what you mean by;

1 Avoid extending a class unless it was designed to be extended?
a) what's one bad idea that would make it a bad idea with an eye toward
extending.

b) one good idea with an eye toward extension.

and

2. Use a base class if you need to include implementation detail
a) you mean implementation details get fleshed out in the derived classes?
Or are you saying certain implementation details should be in the base class
as opposed to somewhere else?

I really appreciate the your responses. Fortunately I wasn't the architect
on a recent project but I found out you can be thrown into a lego pond and
still expect to hook a few things up. -hazz
"Jeff Louie" <je********@yah oo.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
1)Favor containment.
2)Avoid extending a class unless it was designed to be extended.
3)If you have a well defined contract that will be implemented by many
classes use an interface. An interface represents a purely abstract IS_A
relationship.
4)If the contract is evolving, consider using a base class so that you
can provide default implementations of any new methods so that clients
that use your base class can be recompiled without breaking.
5)Use a base class if you need to include implementation details.

That said... every time I try to solve a problem I seem to start by
describing the contract with an interface say IVersionable. Then I end
up writing an abstract class with default implementations as in
AbstractVersion . Then I end up creating concrete classes based on the
Abstract class as in Version : AbstractVersion .

So I would try this pattern: Interface -- AbstractInterfa ce -- Class
which uses both interfaces and base classes and offers both options to
the user of the "interface/class."

Regards,
Jeff

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

Nov 16 '05 #5
I'm certainly no guru, but I've been taking the pragmatic view.

1) You can only inherit code from one base class, but you can implement as
many interfaces as you wish.
2) You have to write code for every interface member, even if the code to
implement it in every class is the same.

In the work I've done so far, it hasn't been too difficult to figure out
when I can write a lot of useful code in a base class, and simply inherit
it, and when most of the methods would have to be overridden anyway, so it
isn't that much more work to have an interface. We save inheritance for the
cases where it's really useful. This has meant writing throwaway code (and
actually throwing it away -- after chopping out the useful bits and putting
them where they really belong), but that's how you learn to program.

And yes, as someone else mentioned, don't forget about containing things. I
have found events very useful for that. If there is a set of functionality I
might like to inherit, but can't because I'm already inheriting from
something else, and I have only a couple methods that would need overrides
(if I were able to inherit from it), then I can create a class that exposes
those methods as events. Then my main class can get the functionality by
having a variable of that type... maybe setting some properties to tell it
what it needs to know about the main class, and handle the events rather
than overriding methods.

-Rachel

"Hazz" <ha**@nospamero osonic.net> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Are there any good references/articles/books which provide clarity toward
my insecurity still on deciding how to model a complex system? I still
feel uncomfortable with my understanding, even though I have worked with
these systems on when to decide to use interfaces (and how they should be
developed) as opposed to or complemented by the use of inheritance from
base classes.
If I am thinking from the point of view of some specific activity and I
begin to think at the method level, how do I think about its
implementation at a code syntax level as to whether that method is
obtained through inheritance from a base class, or if I obtain it via an
interface which I have also inherited from?
Thank you. -hazz

Nov 16 '05 #6
Hi Hazz... Inline
a) what's one bad idea that would make it a bad idea with an eye toward extending.<
If you have a verified collection class that is not designed to be
extended and a user of your class adds setters and getters that do not
verify and corrupt the collection. So inheritance breaks encapsulation.
one good idea with an eye toward extension.< An AbstractVersion class with an == operator and a default virtual
Equals method designed to be overridden by the user. The user can
override the Equals method, thus changing the implementation of the ==
operator!
a) you mean implementation details get fleshed out in the derived

classes? Or are you saying certain implementation details should be in
the base class as opposed to somewhere else?<

C# only supports single inheritance of implementation so your choices
are to extend from a class with implementation or contain a class with
implementation and forward (delegate) calls to the contained class or
just write all the implementation from scratch.

Now the interesting decision is if you should extend from an incomplete
class or define a method that takes an interface that defines the
incomplete methods. So if you have a generic sort routine that is
incomplete and requires a Compare method specific to a user class you
can either create an abstract class with an abstract Compare method or
you can declare a Sort method that takes an IComparable object. In the
first case the user extends the AbstractClass and provides a concrete
Compare method. In the second case the use creates a class that
implements IComparable with a Compare method and passes the IComparable
obect to the Sort method. So
1) MySortableColle ction : AbstractSortabl eCollection
mySortableColle ction.Sort()
2) MyComparableCla ss : IComparable
mySortableColle ction.Sort(myCo mparableClass)
Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7
Absolutely fascinating. Events as another means of reuse. Legos supreme. I
don't understand that yet but maybe it has to do with the delegate or type
safe function pointer which allows a level of indirection to sidestep the
inheritance constraints. You mean I am going to have to learn how to wire up
my own events rather than limit myself to only those times when Visual
Studio does it for me automagically?

So interfaces allow you to flatten the structure?...to inherit useful
functionality by virtue of interface multiple inheritance.
Interfaces allow you to compose if you understand which role each member of
the band plays. I can create new members for my band, adding a horn section
if need be.
And inheritance being the vertical dimension where I can think about things
like, "o.k., by adding more generally used functionality higher up the
hierarchy with the base being highest, then its about using the most derived
class, the lowest member, to be consumed by most clients typically becuase I
have something useful to offer. If I do it right, maintainability is
achieved when I can change some functionality high up the chain in one place
rather than having to fly all over the landscape having to touch down
everywhere there is a component which inherited from it. Maybe just by
adding a constructor with another signature to pass an additional parameter
up the ctor chain, I can make adjustments to my application without really
doing any major design damage. The idea is to be creative right? And we can
have fun doing this? Thanks Rachel.

"Rachel Suddeth" <ra****@bldhoun d.com> wrote in message
news:e$******** ******@TK2MSFTN GP10.phx.gbl...
I'm certainly no guru, but I've been taking the pragmatic view.

1) You can only inherit code from one base class, but you can implement as
many interfaces as you wish.
2) You have to write code for every interface member, even if the code to
implement it in every class is the same.

In the work I've done so far, it hasn't been too difficult to figure out
when I can write a lot of useful code in a base class, and simply inherit
it, and when most of the methods would have to be overridden anyway, so it
isn't that much more work to have an interface. We save inheritance for
the cases where it's really useful. This has meant writing throwaway code
(and actually throwing it away -- after chopping out the useful bits and
putting them where they really belong), but that's how you learn to
program.

And yes, as someone else mentioned, don't forget about containing things.
I have found events very useful for that. If there is a set of
functionality I might like to inherit, but can't because I'm already
inheriting from something else, and I have only a couple methods that
would need overrides (if I were able to inherit from it), then I can
create a class that exposes those methods as events. Then my main class
can get the functionality by having a variable of that type... maybe
setting some properties to tell it what it needs to know about the main
class, and handle the events rather than overriding methods.

-Rachel

"Hazz" <ha**@nospamero osonic.net> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Are there any good references/articles/books which provide clarity toward
my insecurity still on deciding how to model a complex system? I still
feel uncomfortable with my understanding, even though I have worked with
these systems on when to decide to use interfaces (and how they should be
developed) as opposed to or complemented by the use of inheritance from
base classes.
If I am thinking from the point of view of some specific activity and I
begin to think at the method level, how do I think about its
implementation at a code syntax level as to whether that method is
obtained through inheritance from a base class, or if I obtain it via an
interface which I have also inherited from?
Thank you. -hazz


Nov 16 '05 #8

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

Similar topics

9
13274
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred form for this in Python? In a staticly typed language like c++ or java, I'd describe the interface first, then create the classes eithered derived from that ABC or implementing that interface (same thing really). This was the first thing I...
4
8205
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by making my contnent dlls implement an interface created in vb6. The viewer application is bound to this interface. This way, I am able to add Content without redeploying the dlls (I just have to add the new dlls). I want to write new content for...
4
2109
by: christopher diggins | last post by:
A feature that I find signficantly missing in C# is the ability to write functions in interfaces that can call other functions of the interface. Given an interface ISomeInteface the only way we can write a general purpose function to operate on all objects which implement that interface is through the following style of declaration (in the following code snippets i is an interface variable of type ISomeInterface) : SomeStaticClass {...
21
13849
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash elements, not sort them). - IDictionary is definatly not setlike. Although I can, of course, define...
10
2981
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
6
6080
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives using System; using System.Collections.Generic; using System.Text;
12
2845
by: Meya-awe | last post by:
I am puzzled, what is the purpose of an interface? How does it work, what i mean is how does the compiler treats this? Why when we talk about separating user interface from business logic, an interface is declared, what is it's purpose? thanks, BRAMOIN *** Sent via Developersdex http://www.developersdex.com ***
4
20641
by: Raja Chandrasekaran | last post by:
Hai friends, I really wonder, If the interface does not have any definition, Y do we need to use interface. You can then only we can use Multiple inheritance. I really cant understand, Just for declararion y do we need to use interface. Anyhow the method name and definition ll be in the derived class. Instead of that we can do all code in the derived class itself right...? Then y these concept came. If anybody know, please explain me...
52
20912
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
9647
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8988
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.