You must Sign In to post a response.
  • Category: .NET

    Need clear understanding on Abstract, Interface in C#

    HI Friends,

    Am facing some basics understanding issue on Abstract, Interface in C#, i got some explanations by surfing and some friends, but nothing make me clear about these concepts. Can any one give me link or some clear explanation?

    Thanks in advance.
  • #767553
    refer

    http://gktoall.com/WPF/C_Sharp_Question_Answers-5.htm

    Thanks
    Shivshanker Cheral
    "If you share your assets (money etc..) it will decrease, But if you share your knowledge it will increase!!! ".

  • #767561
    Abstract class:
    Abstract class can contain both abstract methods and non abstract methods.
    If a class contains at least one abstract method then that class must be declared as abstract class.
    Abstract class can inherit from another abstract class and can inherit from more than one interface.
    Abstract methods must be implemented in derived class.
    Object cannot be created for abstract classes, reference can be created.

    when to use: If you want a method to be implemented in all its child classes the that method should be declared as abstract method. If a class contains at least one abstract method the that class should be declared as abstract class.

    public abstract class myabstract: IA, IB //IA and IB are two interfaces
    {
    public abstract viod m1(); //abstract method
    public void m2() //non abstract method
    {
    //some logic
    }
    }

    Interface:
    Interface contain only abstract methods, which are public abstract by default.
    Interface can implement another interface but cannot implement abstract class.
    Object cannot be created for interface, reference can be created.

    When to use:
    If you want to use multiple inheritence concept then we can use intefaces to implement a class with more than one interface.
    public interface ISample:IMysample,IMysample2 //it is implementing two interfaces its not possible in normal class.
    {
    void mymethod();
    void mymethod2();
    }

    Sridhar Thota.
    Editor: DNS Forum.

  • #767569
    Hi,

    Find out the difference between two then you can easily understand what is what and when to use that, first go through the below link to differentiate both "codeproject.com/Articles/11155/Abstract-Class-versus-Interface"

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767576
    Abstract and Interface is really a most heard topic I think, see below explanation about them.
    In simple words, Abstract can not be instantiated, An abstract class is only to be sub-classed (inherited from). The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
    Note, An interface is not a class, An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body, The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767728
    Hai Murugesan,
    The first thumb rule for the abstract class and interface is that when the class is not complete means abstract, we must have the define the class as abstract class. It means the class is having at-least one incomplete method/property/delegate etc.
    1. Abstract class can contains abstract as well as concrete members while interface is collection of abstract members only.
    2. In Abstract class, access specifiers are allowed, while in interface, they are not.
    3. When using Abstract class into the child class, all the abstract members must be overridden. All the members must have the override method in the child class. While in interface, all the interface members must be implemented when using the interface in a class.
    4. When the scope is limited, we use abstract class while if the scope is broad, interface are better to use.
    For more related concepts, you can go through the below link:

    http://pawantechit.blogspot.my/2011/09/oops-concepts-when-and-how-to-use.html

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments