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

    Differnace between Abstraction and interface

    What is Abstarction , Why we need to use abstract class

    already we have over riding methods , we can use over riding class , why should we go for abstraction class,Is there any specific reason ?
  • #761846
    I will explain with one simple example

    Main concepts for using abstract class are

    1. Can not create instance
    2. It has implementation and declarations.

    Let us consider one organization. It has two type of employes Permanant and Contract.

    1. They have lot of common properties and functionalities. Ex. Name, Address etc...
    2. They have some difference like Leave Calculation, Salary calculation

    In the Above case we can create once abstract class "Employee". In the abstract class we can implement Name, Address etc and we can declare Salary calculation. These salary calculation will be implement in the derived class.

    OK! now you can ask the question, that we can create normal class and create one interface. Why we have to go for abstract class correct?.

    We have only two type of employees (Permenent/Contract). We can allow to create instance of Permenent/Contract. But we should not allow the developer to create the instance of "Employee". So that we are going for abstract class.

    If we go for abstract class we do not need to create interface also. So the consistency of method name can also maintin..

    I think you got my point

    By Nathan
    Direction is important than speed

  • #761847
    Hai Praveen,
    Thanks for asking the nice question. You are right that why do we need to use Abstraction if the overridden method can fulfill all the requirements.
    You can get the answers at the below link:

    http://pawantechit.blogspot.in/2013/09/scenario-based-questions-in-net.html

    You can check the Question.10 of this link which says the question "10. Why do we need abstract class or abstract members? Can't we simply write the general methods and fulfill our requirements? Can we get any advantage by implantation of abstract members? As per the abstract class scenario, let's say we have an abstract method called Show () in the abstract base class. Now if I am inheriting this base class to a child class, we need to override this abstract method to the child class. And then we will call this method by creating instance of child class.Now if we only have the method in the child class and then create the instance and call the same method? Then why abstract class method? Is that method doing anything here?"
    Hope it will be helpful to you.

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

  • #761850
    Hi

    Let me first tell you when we use abstract class.

    If you want a method to be overridden in all the child classes what you will do?
    Here abstract comes in to picture.

    Just declare that method as abstract method, by declaring abstract method in parent class you are forcing all the child classes to override the abstract method.

    If you have at least one abstract method then that class should be declared as abstract class.

    Rules of abstract class:
    1.We cannot create object of abstract class.
    2.Abstract class can contain both abstract and non abstract methods.

    Regards

    Sridhar Thota.
    If you learn't from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.

  • #761880
    Interfaces are used to achieve multiple inheritance in c# programming.

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

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

    public interface ISample:IMysample
    {
    void mymethod();
    void mymethod2();
    }


    Regards

    Sridhar Thota.
    If you learn't from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.

  • #761948
    Hello Praveen,

    Main difference :

    An Abstract class doesn't provide full abstraction but an interface does provide full abstraction

    Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.

    An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.

    Abstraction Example :

    class program
    {
    abstract class animal
    {
    public abstract void drink();
    public void swim()
    {
    Console.WriteLine("fish can swim");
    }
    }
    class fish : animal
    {
    public override void drink() { Console.WriteLine("fish can drink water"); }
    }
    static void Main(string[] args)
    {
    fish fs = new fish();
    animal thePet = fs;
    thePet.drink();
    fs.swim();
    }
    }


    Interface Example :

    class Demo : abc
    {
    public static void Main()
    {
    System.Console.WriteLine("Interfaces");
    xyz();
    }

    public void xyz()
    {
    System.Console.WriteLine("In xyz");
    }
    }

    interface abc
    {
    void xyz();
    }


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments