15

Difference between Interface, abstract class, sealed class, static class and partial class in c#? If all classes available in vb.net?

saran
  • 1,273
  • 4
  • 19
  • 34

6 Answers6

46
  • 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.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
14
  • 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.

Paul Keister
  • 12,851
  • 5
  • 46
  • 75
3

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.

Sumit
  • 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
2

Following are the differences between abstract and interface:

  1. Abstract classes have method declaration as well as method definition whereas interfaces have method declaration only.

  2. Abstract classes are known as partial abstract classes whereas interfaces are known as fully abstract classes.

  3. Abstract class features are inherited by child classes whereas interface features have to be implemented in implementing classes.

  4. Abstract classes support access specifiers whereas interfaces don't support access specifiers.

  5. Abstract classes have normal variables as well as constant variables whereas interfaces do not have variables.

  6. We can write constructors in abstract classes whereas we can't write constructors in interfaces.

Grant Palin
  • 4,546
  • 3
  • 36
  • 55
SouravM
  • 209
  • 2
  • 6
  • 14
2

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
0

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.

  • 1
    I 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