开发者

Difference between Interface, abstract class, sealed class, static class and partial class in C#? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.

Difference between Interface, abstract class, sealed class, static class and partial cla开发者_如何转开发ss in c#? If all classes available in vb.net?


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


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


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜