472,352 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

When would you use an abstract class and when an interface?

When would you use an abstract class and when an interface?

whats the abstract class.?

please give me the sample .
Sep 23 '08 #1
5 12741
Damodhar wrote:
When would you use an abstract class and when an interface?
The advantage of an abstract class is that you can (partially) implement
the class. The advantage of an interface is that you can implement
multiple interfaces, whereas you can not extend multiple abstract classes.

Of course, you are not the only one with this question:
http://www.google.nl/search?q=abstract+class+interface
Sep 23 '08 #2
On 23 Sep, 11:32, Damodhar <damu...@gmail.comwrote:
When would you use an abstract class and when an interface?

whats the abstract class.?

please give me the sample .
I'd use an abstract class if I were writing in a strongly typed OO
language and an interface the rest of the time. Why do you ask?

C.
Sep 24 '08 #3
..oO(C. (http://symcbean.blogspot.com/))
>On 23 Sep, 11:32, Damodhar <damu...@gmail.comwrote:
>When would you use an abstract class and when an interface?

whats the abstract class.?

please give me the sample .

I'd use an abstract class if I were writing in a strongly typed OO
language and an interface the rest of the time.
What has the typing to do with it? Abstract classes and interfaces are
there for quite different purposes. Both have their uses even in PHP.

Micha
Sep 24 '08 #4
NC
On Sep 23, 3:32 am, Damodhar <damu...@gmail.comwrote:
>
When would you use an abstract class and when an interface?
Since an abstract class contains implementations and an interface does
not, the answer should be obvious: if you have runnable code, it does
not belong in an interface.
whats the abstract class.?
It's a class for which instantiation is not allowed. In other words,
if class X is defined as abstract, attempting to create an object of
type X will result in an error.

Cheers,
NC
Sep 24 '08 #5
On Sep 23, 11:32*am, Damodhar <damu...@gmail.comwrote:
When would you use an abstract class and when an interface?

whats the abstract class.?

please give me the sample .
An abstract class is useful when you need a substantial amount of
functionality to be identical across the subclasses, for example in a
shopping cart with multiple methods of payment available, you could
have an abstract class which defines a generic payment method, and
have subclasses inherit from the superclass for each actual payment
method you want to support (paypal, credit card, account, etc). The
mechanics of how a payment is authorized would be different for each
subclass, but they all perform essentially the same function - they
validate that a user can prove that they can pay for the goods or
services in question.

An example of where an interface is useful is where you have unrelated
items that need to provide some similar functionality in a uniform
way. For example, you might have a CMS where articles are stored in a
database, but where the system caches them to disc as well as HTML
pages until the article in the database is modified, at which point
the physical file is deleted until the next time someone access the
copy in the database. Your CMS might also support the ability for
users to upload images, PDFs, etc to be stored for access on the disc,
but you definitely don't want these files to be deleted as the copy on
the disc represents the file itself and not a cached version. In this
case, you could create a Cacheable interface that says what methods a
class which is cached to disc needs to implement, while leaving it up
to the class itself to implement them. This makes more sense as
classes that represent different kinds of data almost certainly need
to implement their caching scheme (if any) differently.

Every class that allows caching would be defined as Class <name>
implements Cacheable, which is something you can then check for in
your code. Less experienced coders might test the class of an object
they are working with by getting the class and processing the result
with a big switch statement. This isn't the correct approach because
it means that you're assuming that certain classes objects implement
certain functionality, and if you add a new class to the system you
need to modify every switch statement in your software to take it into
account. If yo uimplement an interface you can test if an object
implements that interface with the instanceof keyword.

if ($thisObject instanceof Cacheable)
{
// Manage item's cache
}

This approach is better because it eliminates the switch statement and
thus makes your software easier to maintain. If you add a new class
to the system that also implements its own caching scheme then you
just need to declare that it implements Cacheable. As the interface
requires all classes to implement it to declare the methods specified
in the interface you can be sure that any class that implements
Cacheable will provide certain methods for you to use. Your code
doesn't need to know how the class implements these methods, just that
it does implement them.

These concepts are somewhat trickier to explain than to actually learn
to use I'm afraid, hopefully I've got the basic ideas across well
enough for you to figure them out for yourself.
Sep 25 '08 #6

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

Similar topics

6
by: Martyn Lawson | last post by:
Hi, I am currently working as an Analyst on a .NET Web Project using ASP.NET and C#.NET. I have a couple of, at least what should be, quick...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from...
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...
1
by: D Witherspoon | last post by:
Coming up with a scenario here. For example there is the standard .NET MailMessage class. I am creating a project (let's call it CommonBase)...
7
by: gordon | last post by:
Hi I am working through some course notes for a msdn training course in C# and I am a little stuck with the differences between an abstract class...
1
by: Rich | last post by:
Hello, my project (vb2005) contains several classes that each produce lists of data which get stored/displayed in ado.net tables that have the...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does...
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.