Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have to know particular used of interface and abstract class
Posted 5-Feb-13 6:44am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Is it really that hard to google. Interface vs Abstract class[^]
 
This is also a reposted question. Please don't post the same question multiple times. If you have an update to a question, use the improve question widget to update your existing question instead of submitting a new one.
  Permalink  
v2
Comments
Abhinav S at 5-Feb-13 11:56am
   
5.
Sergey Alexandrovich Kryukov at 5-Feb-13 12:10pm
   
This is a good link, my 5, but there is more to it. Please see my answer.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

The two concepts are different, but related.
Let's start with an Interface.
C# will only allow you to derive a new class form a single class:
public class MyBaseClass {}
public class MyDerivedClass : MyBaseClass {}
You cannot derive a class from two or more concrete classes:
public class MyBaseClass1 {}
public class MyBaseClass2 {}
public class MyDerivedClass : MyBaseClass1, MyBaseClass2 {}
So how do you add a grouping so that you can treat a class as a member of a club? For example, how do you say "this new class is based on a Control, but it also knows how to act like a List of items"?
 
The answer is to use an Interface. This lays out the rules of the "Club" (it defines which properties and methods you must implement in order to join) and does not in any way provide an code at all to either help or hinder you. In the example above, the IEnumerable Interface requires that you implement the GetEnumerator method, and when you do allows you all the priveliges ofthe club membership - for example, your class can now appear in a foreach loop.
            MyClass mc = new MyClass();
            foreach (string s in mc)
                {
                }
            }
 
        public class MyClass : IEnumerable<string>
            {
            public IEnumerator GetEnumerator()
                {
                throw new NotImplementedException();
                }
            }
 

An abstract class is similar in a way, but it is a class in itself so you can derive from it, but you can't derive from any other class at the same time. It can implement properties and methods in the same way as a "normal" class, but it doesn't have to. The big difference between an abstract class and a "normal" (or concrete) class is that you cannot under any circumstances create an instance of the abstract class - you can declare a reference variable, but you can only ever create instances of derived classes.
For example, you could have an abstract class "Fruit" which is the base for the "Apple", "Pear", and "Orange" classes.
Fruit f = new Apple();
Orange o = new Orange();
f = o;
Are all legal.
But you can't say:
Apple a = new Apple();
Orange o = new Orange();
o = a;
Because they are both Fruit, but they aren't the same fruit.
 
You use an abstract class to create a class that has no instances - but groups together instances, and allows you to pass any of the instance classes through to a method which expects the abstract base class.
 
In the real world, you could define an abstract class "OutputDevice" which says it wants a Write method which takes a string, and define a FileOutputDevice and a TextBoxOutputDevice which are derived from the abstract. Provided both concrete classes implement the Write method, you can write a single method which takes an OutputDevice as teh parameter and prints the contents of a book to it. It will the=n work without changes to the code. And you could add a ConsoleOutputDevice class and the method still needs no changes to work!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

An abstract class can contain some implementation. An interface does not have any implementation.
 
Multiple interfaces can be inherited in C#. Only a single abstract class can be inherited.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I already answered your other question, so I am copy/pasting the old answer:
 
I think that if you do a search you will see tons of documents explaining the difference. But I will resume it as this:

Interfaces can't have a default implementation for any method. Abstract classes can.
A class can implement any interface, but if you already have a class with a base type, it will not be capable of inheriting from an abstract class.

When to use each one?
In general I use both.
I create an interface so all methods can be reimplemented in many different ways (that helps in remoting, for example, or to create fake implementations for testing purposes).
But if some method is probably going to be implemented in the same way by many classes, I create an abstract class that already implement such method but let other methods be fully virtual.

A sample? If you look at the sample of my latest article Creating your own animation format[^] you will see the following:

IFrameReader. This interface has a non-generic untyped version that can be used if you have the right objects but don't know the generic type at compile time (that is, you pass parameters as objects).
Then it has a generic version that you can use and avoid casts if you know the right types at compile time (the ManagedBitmap<Rgb24> for example).

But, to those who want to implement a FrameReader class and don't want to lose time implementing both interfaces (as the untyped version will simple redirect to the typed version), there is the FrameReader class that already implements both interfaces. The untyped (non-generic) version simple redirects to the typed version, which keep those typed methods as abstract.

So, inheriting from the FrameReader class you get an already implemented IFrameReader but you still need to implement the methods that make the IFrameReader<T> work.

I hope this is clear enough.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100