Thursday, October 12, 2017

Interface and Abstract class in c#

Interfaces is very general concept of object oriented programming. Here are few fundamentals rules associated with interfaces in c# -

- Interfaces can contain methods, properties, events, indexers, or any combination of those four member types.
- An interface can't contain constants, fields, operators, instance constructors, finalizers, or types.
- Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.
- Interfaces can implement other interfaces.
- A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces implement. However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (class ClassName : InterfaceName). If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface. However, the derived class can reimplement the interface members instead of using the inherited implementation.

Abstract Class -
Abstract class is class which has atleast 1 abstract method. Abstract class can inherit from non abstract class. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.
public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}

public abstract class E : D
{
    public abstract override void DoWork(int i);
}

public class F : E
{
    public override void DoWork(int i)
    {
        // New implementation.
    }
}
If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method—in the previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.

Sealed Class:-
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.
A method, indexer, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed. This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed keyword before the override keyword in the class member declaration.
The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.

15 comments:

  1. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  2. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  3. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Big data Hadoop Related information please visit our website hadoop classes in pune page!

    ReplyDelete
  4. thanks for Sharing such an Awesome information with us.

    I learned World's Trending Technology from certified experts for free of cost.i Got job in decent Top MNC Company with handsome 14 LPA salary, i have learned the World's Trending Technology from Python training in pune experts who know advanced concepts which can helps to solve any type of Real time issues in the field of Python. Really worth trying Freelance seo expert in bangalore

    ReplyDelete

  5. Thanks for sharing.Your information is clear with very good content.This was the exact information i was looking for.keep updating more.If you are the one searching for big Data certification courses then visit our site big data certification bangalore

    ReplyDelete
  6. Peerless and helpful post..
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    Please visit our site,
    buylogo

    ReplyDelete