473,406 Members | 2,343 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,406 software developers and data experts.

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 2237
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**********@discussions.microsoft.com> a écrit dans le
message de news:29**********************************@microsof t.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
"implementation-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: NonBlockingStream 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 CreateWaitHandle 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**********@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.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**********@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.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 "containment by ownership" making
Car
an example of a composite class.*

Containment can also be accomplished using references to external
objects.
"Containment 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
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...
9
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...
10
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...
18
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
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
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...
7
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...
6
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...
5
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...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
0
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...
0
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...
0
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,...

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.