Difference between Interface, abstract class, sealed class, static class and partial class in c#? If all classes available in vb.net?
-
12Read a book, or an introduction to C#. Or if you are brave, the ECMA spec. – leppie Jan 21 '11 at 06:38
-
2@leppie - The ECMA spec is surprisingly easy to follow in my opinion. – ChaosPandion Jan 21 '11 at 06:39
-
@ChaosPandion: Yes, I agree, but not for a novice that do not understand the underlying concepts. – leppie Jan 21 '11 at 06:40
-
2@ChaosPandion haha, I often found it simpler than msdn docs :) – nawfal May 15 '13 at 09:01
6 Answers
abstract class
Should be used when there is a IS-A relationship and no instances should be allowed to be created from that abstract class. Example: An Animal is an abstract base class where specific animals can be derived from, i.e. Horse, Pig etc. By making Animal abstract it is not allowed to create an Animal instance.interface
An interface should be used to implement functionality in a class. Suppose we want a horse to be able to Jump, an interface IJumping can be created. By adding this interface to Horse, all methods in IJumping should be implemented. In IJumping itself only the declarations (e.g. StartJump and EndJump are defined), in Horse the implementations of these two methods should be added.sealed class
By making Horse sealed, it is not possible to inherit from it, e.g. making classes like Pony or WorkHorse which you like to be inheriting from Horse.static class
Mostly used for 'utility' functions. Suppose you need some method to calculate the average of some numbers to be used in the Horse class, but you don't want to put that in Horse since it is unrelated and it also is not related to animals, you can create a class to have this kind of methods. You don't need an instance of such a utility class.partial class
A partial class is nothing more than splitting the file of a class into multiple smaller files. A reason to do this might be to share only part of the source code to others. If the reason is that the file gets too big, think about splitting the class in smaller classes first.

- 15,025
- 28
- 93
- 119
-
1excellent and very easy. could not find so easy like that. thanks a lot – Syed Md. Kamruzzaman Mar 10 '16 at 02:51
Interface: method definitions only
Abstract class: some method implementations, some methods abstract (method definition only)
Sealed class: A class from which you may not inherit
Static class: a class with only static methods (no instances exist, all methods may be called without an instance)
Partial class: A class that is defined in 2 or more separate class definitions in different modules.
Yes, they are all available in both C# and VB, although VB uses different keywords in some cases.

- 12,851
- 5
- 46
- 75
I guess following link will be useful for you.
http://letschattechnology.com/interface-vs-abstract-classes/
the basic logical difference is you create abstract class when there is a relation between two classes that will inherit the abstract class and you create interface for the classes which are not related to each other but do have some common functionality.

- 2,932
- 6
- 32
- 54
-
Did anyone else click on this link?? It didn't take me to anything code related... was a rather naughty link for me lol – fifamaniac04 Apr 07 '15 at 21:26
-
1@fifamaniac04: I am sorry, link had changed. I have updated the link. You should be able to see that post now. – Sumit Apr 08 '15 at 05:29
Following are the differences between abstract and interface:
Abstract classes have method declaration as well as method definition whereas interfaces have method declaration only.
Abstract classes are known as partial abstract classes whereas interfaces are known as fully abstract classes.
Abstract class features are inherited by child classes whereas interface features have to be implemented in implementing classes.
Abstract classes support access specifiers whereas interfaces don't support access specifiers.
Abstract classes have normal variables as well as constant variables whereas interfaces do not have variables.
We can write constructors in abstract classes whereas we can't write constructors in interfaces.

- 4,546
- 3
- 36
- 55

- 209
- 2
- 6
- 14
-
"interfaces don't support access specifiers." - I used internal explicitly and public by default. – priyanka.sarkar Feb 07 '15 at 16:15
A normal
class can be instantiated at runtime to form an Object with fields (fields are properties, functions, events, etc). A normal
class can also be inherited/sub-classed.
Adding one of the extra keywords change the way the class works.
- Adding
public, private, protected
changes the way other code can see and use this class. - Adding
static
to a class means you can't create an instance with the new keyword but you can only access it through static function. Example:String.IsNullOrEmpty()
. - Adding
sealed
to a class means no other class can inherit the 'sealed' class.
Interfaces are contracts
that say an implementing class will supply some functionality. The IDisposable
interface says that all classes implementing it will have a Dispose function.

- 34,448
- 50
- 182
- 322

- 3,392
- 2
- 27
- 54
In abstract class can provide more functionality without affecting child class. In interface,if we add any method to interface ,then it will affect all the child class.
-
1I think you should elaborate more your post to ensure it provides a proper answer to OP. Please also have a look to our FAQ : http://stackoverflow.com/faq. Good luck. – ForceMagic Nov 12 '12 at 07:08