473,566 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Abstract Classes vs Interfaces

Hi ,

Please help me by advising an real life scenario where Abstract Classes
should be used over Interfaces or vice versa . Whats the basic difference
between Abstract Class and interface other then instantiation ?

Many Thanks

Vishal Gandhi
Jul 21 '05 #1
8 2254
See :
http://msdn.microsoft.com/library/de...Interfaces.asp

As an exemple for this exceprt :

"Abstract classes should be used primarily for objects that are closely
related, whereas interfaces are best suited for providing common
functionality to unrelated classes. "

see the Stream class (that is abstract, all streams having the same
behavior) and the IEnumerable interface (that is implemented by classes
having nothing else in common).

Patrice
--

"Vishal Gandhi" <Vi**********@d iscussions.micr osoft.com> a écrit dans le
message de news:29******** *************** ***********@mic rosoft.com...
Hi ,

Please help me by advising an real life scenario where Abstract Classes
should be used over Interfaces or vice versa . Whats the basic difference
between Abstract Class and interface other then instantiation ?

Many Thanks

Vishal Gandhi

Jul 21 '05 #2
Patrice wrote:
"Abstract classes should be used primarily for objects that are closely
related, whereas interfaces are best suited for providing common
functionality to unrelated classes. "
Abstract classes should (in my opinion) only be used as
"implementa tion-helpers" skeletons of implementations which are just
missing some important parts.

Interfaces should be used to expose behavioural traits.

To use an abstract class to expose behavioural traits restricts the
implementations of that interface.

You can argue, that using an abstract class reduces the amount of
classes defined, but you save at-most 1 interface-declaration.
see the Stream class (that is abstract, all streams having the same
behavior) and the IEnumerable interface (that is implemented by classes
having nothing else in common).


I don't think Stream is a good example. A stream is a concept, an
idea... and not related to some specific way of implementation.

I would much have preferred Stream as an interface. and:

- a helper-class: ImpotentStream that would return false on all "CanX"
and throw on all other methods, allowing you to easily override just the
bits you use.
- an abstract helper-class: BlockingStream that required you to
implement CanRead, CanWrite, Read, Write, Flush & Close
- another abstract helper-class: NonBlockingStre am that required you
to implement CanRead, CanWrite, BeginRead, BeginWrite & GetWaitHandle
- Adapters, implementing blocking IO in terms of non-blocking and the
inverse.

This increases the number of classes, but vastly decreses the complexity
of a working Stream-implementation.

The whole Stream "interface" smells bad, it smells like a .NET wrapper
for whatever C code was in win32.

It's basically a union of handy operations on "things". What is
"SetLength" doing on a stream? Why is CreateWaitHandl e not just
"WaitHandle { get; }" and *why* is it protected. Why are there
operations to read/write single bytes.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Jul 21 '05 #3
Hi Vishal,

"The basic difference between an Abstract class and an interface is that
Abstract class allows Concrete method(methods that have implementation) but
interface doesn't. The second difference is in terms of inheritance. A class
can inherit from only one class but can implement any number of interfaces.
One more difference is that interface doesn't allow variables/constants to
be declared but abstract class allows for declaration of variables and
constants.

You should consider using interfaces when your design is complete in itself.
i.e., no further methods need to be added later on to the interface as any
newly added method in interface need to be implemented in all the classes
that implement that interface. On the other hand if we need to add a new
method to an abstract class, we can provide default implementation of that
method in abstract class and there is no need for existing implementing
classes to be changed in that case"

Check these links for more info on when to use what...

http://www.dotnetspider.com/Question9463.aspx
http://www.dotnetspider.com/technolo...ages/1041.aspx

HTH,
Need any help, do post a msg back..

Happy Coding

"Vishal Gandhi" <Vi**********@d iscussions.micr osoft.com> wrote in message
news:29******** *************** ***********@mic rosoft.com...
Hi ,

Please help me by advising an real life scenario where Abstract Classes
should be used over Interfaces or vice versa . Whats the basic difference
between Abstract Class and interface other then instantiation ?

Many Thanks

Vishal Gandhi

Jul 21 '05 #4
First lets agree that an interface is analoguos to a C++ _pure_ virtual
class
with no implementation. An abstract class implies that it cannot be
instantiated but is designed to be extended. So the question becomes
when
to write an abstract class with a partial implementation vs when to
write a
pure virtual class with no implementation. And you ask for an example.
The
answer is to use an abstract class with a partial implementation when
you
must:

Defer the Final Implementation to Another More Knowledgeable Class

The quest is to find a situation where a lot of complicated logic is
implemented but the final implementation must be deferred to a more
knowledgeable class. The classic example is a quick sort routine. You
know
the algorithm but you cannot write the final implementation without
knowing
the actual ordinal behaviour of the type to be sorted. So you can write
an
abstract class with a powerful partial implementation leaving the
abstract
Compare(obj, obj) method without an implementation. The user of your
abstract class simply supplies a concrete implementaton of the Compare
method reusing the power of you quick sort algorithm. An alternative is
to
write a Sort method that takes an IComparable object that defines the
Compare method.

The downside of interfaces is that they are difficult to update. so.

In summary, if you have a well defined contract that will be implemented
by
many classes use an interface. If the contract is evolving, consider
using a
base class. Use a base class if you need to include implementation
details
such as in our generic quick sort algorithm.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jul 21 '05 #5
Vishnu ,

Many Thanks for explanation . This gives more clarity , also if you will be
able to provide some calrification on Aggregate class with an example it will
help.

Cheers
Vishal

"Vishnu-Chivukula" wrote:
Hi Vishal,

"The basic difference between an Abstract class and an interface is that
Abstract class allows Concrete method(methods that have implementation) but
interface doesn't. The second difference is in terms of inheritance. A class
can inherit from only one class but can implement any number of interfaces.
One more difference is that interface doesn't allow variables/constants to
be declared but abstract class allows for declaration of variables and
constants.

You should consider using interfaces when your design is complete in itself.
i.e., no further methods need to be added later on to the interface as any
newly added method in interface need to be implemented in all the classes
that implement that interface. On the other hand if we need to add a new
method to an abstract class, we can provide default implementation of that
method in abstract class and there is no need for existing implementing
classes to be changed in that case"

Check these links for more info on when to use what...

http://www.dotnetspider.com/Question9463.aspx
http://www.dotnetspider.com/technolo...ages/1041.aspx

HTH,
Need any help, do post a msg back..

Happy Coding

"Vishal Gandhi" <Vi**********@d iscussions.micr osoft.com> wrote in message
news:29******** *************** ***********@mic rosoft.com...
Hi ,

Please help me by advising an real life scenario where Abstract Classes
should be used over Interfaces or vice versa . Whats the basic difference
between Abstract Class and interface other then instantiation ?

Many Thanks

Vishal Gandhi


Jul 21 '05 #6
Many Thanks Jeff

"Jeff Louie" wrote:
First lets agree that an interface is analoguos to a C++ _pure_ virtual
class
with no implementation. An abstract class implies that it cannot be
instantiated but is designed to be extended. So the question becomes
when
to write an abstract class with a partial implementation vs when to
write a
pure virtual class with no implementation. And you ask for an example.
The
answer is to use an abstract class with a partial implementation when
you
must:

Defer the Final Implementation to Another More Knowledgeable Class

The quest is to find a situation where a lot of complicated logic is
implemented but the final implementation must be deferred to a more
knowledgeable class. The classic example is a quick sort routine. You
know
the algorithm but you cannot write the final implementation without
knowing
the actual ordinal behaviour of the type to be sorted. So you can write
an
abstract class with a powerful partial implementation leaving the
abstract
Compare(obj, obj) method without an implementation. The user of your
abstract class simply supplies a concrete implementaton of the Compare
method reusing the power of you quick sort algorithm. An alternative is
to
write a Sort method that takes an IComparable object that defines the
Compare method.

The downside of interfaces is that they are difficult to update. so.

In summary, if you have a well defined contract that will be implemented
by
many classes use an interface. If the contract is evolving, consider
using a
base class. Use a base class if you need to include implementation
details
such as in our generic quick sort algorithm.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***

Jul 21 '05 #7
Thanks Vishal for raising this topic. Thanks to Vishnu-Chivukula and Jeff for
giving the nice explanation of Abstract Classes vs Interface.
"Jeff Louie" wrote:
First lets agree that an interface is analoguos to a C++ _pure_ virtual
class
with no implementation. An abstract class implies that it cannot be
instantiated but is designed to be extended. So the question becomes
when
to write an abstract class with a partial implementation vs when to
write a
pure virtual class with no implementation. And you ask for an example.
The
answer is to use an abstract class with a partial implementation when
you
must:

Defer the Final Implementation to Another More Knowledgeable Class

The quest is to find a situation where a lot of complicated logic is
implemented but the final implementation must be deferred to a more
knowledgeable class. The classic example is a quick sort routine. You
know
the algorithm but you cannot write the final implementation without
knowing
the actual ordinal behaviour of the type to be sorted. So you can write
an
abstract class with a powerful partial implementation leaving the
abstract
Compare(obj, obj) method without an implementation. The user of your
abstract class simply supplies a concrete implementaton of the Compare
method reusing the power of you quick sort algorithm. An alternative is
to
write a Sort method that takes an IComparable object that defines the
Compare method.

The downside of interfaces is that they are difficult to update. so.

In summary, if you have a well defined contract that will be implemented
by
many classes use an interface. If the contract is evolving, consider
using a
base class. Use a base class if you need to include implementation
details
such as in our generic quick sort algorithm.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***

Jul 21 '05 #8
Hi Vishal.. Sure. Looks like you are taking a class! These are good
questions
that make you think. So. Composition is containment by ownership (black
diamond UML). Aggregation is containment by reference (white diamond
UML). The classic example is a car class.

Inheritance represents an IS_A relationship from a generalization to a
specialization. Containment represents a HAS_A relationship between the
whole and a part. So a car IS_A motorized vehicle, but HAS_A radio. The
two
relationships can be expressed in code thusly:

class Radio
{
...
}
class Vehicle
{
...
}
class Car : Vehicle
{
Radio r= new Radio();
}

An instance of Car contains an instance of a Radio so that the lifetimes
of the
radio and car are intertwined. This is "containmen t by ownership" making
Car
an example of a composite class.*

Containment can also be accomplished using references to external
objects.
"Containmen t by reference" can be expressed in code thusly:

class Radio
{
...
}
class Vehicle
{
...
}
class Car : Vehicle
{
private Radio r;
public Car(Radio r) {
...
this.r= r;
}
}
You can then create an instance of Car like this:
class Class1
{
[STAThread]
static void Main(string[] args)
{
Radio r= new Radio();
Car c= new Car(r);
}
}

An example of containment by reference is a read only wrapper class.

Regards,
Jeff
This gives more clarity , also if you will be able to provide some
calrification
on Aggregate class with an example it will help.
*** Sent via Developersdex http://www.developersdex.com ***
Jul 21 '05 #9

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

Similar topics

6
5793
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove the need to have abstract class types in C#? Derived classes from abstract base classes must overrided the abstract method defininition anyway,...
9
7543
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must be implemented. So if you have a class which inherits base class that inherts an interface, then your classes will have a standard. I suppose you...
10
2957
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...
18
3765
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
9
5185
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
8
351
by: Vishal Gandhi | last post by:
Hi , Please help me by advising an real life scenario where Abstract Classes should be used over Interfaces or vice versa . Whats the basic difference between Abstract Class and interface other then instantiation ? Many Thanks Vishal Gandhi
7
4459
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears to be checking that the correct data type is used DataAcess sets an abstract class and methods
6
4021
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it would be useful to have /some/ data defined in such an abstract class for reasons of forming a common block of data existing in or used by all...
5
3001
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if C# even has Iinterfaces. I took some Java programming classes and Interfaces are a main staple of Java. And in VB.Net I use interfaces for setting...
0
7666
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
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
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...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
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
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
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
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
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...

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.