Amit
Amit

Reputation: 22086

Why we should implement Interface?

Implementing Interface just provide the skeleton of the method. If we know the exact signature line of that method, in this case
what is the requirement to implement Interface?
This is the case in which Interface has been implemented

interface IMy
{
    void X();
}
public class My:IMy
{
    public void X()
    {
        Console.WriteLine("Interface is implemented");
    }
}

This is the case in which Interface has not been implemented

public class My
{
    public void X()
    {
        Console.WriteLine("No Interface is implemented ");
    }
}


My obj = new My();
obj.X();

Both the approaches will produce the same result.
what is the requirement to implement Interface?

Upvotes: 4

Views: 513

Answers (7)

supercat
supercat

Reputation: 81337

There are two purposes of inheritance in .net:

  1. Allow derived classes to share the base-class implementations of common functionality
  2. Allow derived-class objects to be substituted for base-class objects anywhere the latter would be accepted.

Unlike some languages (C++, for example) which allow multiple inheritance, .net requires every class to have precisely one parent type (Object, if nothing else). On the other hand, sometimes it's useful to have a class be substitutable for a number of unrelated types. That's where interfaces come in.

An object which implements an interface is substitutable for an instance of that declared interface type. Even though objects may only inherit from one base type, they may implement an arbitrary number of interfaces. This thus allows some of the power of multiple inheritance, without the complications and drawbacks of full multiple-inheritance support.

Upvotes: 3

Renatas M.
Renatas M.

Reputation: 11820

Heres simple example wich helped me to understand interfaces:

interface IVehicle
{
    void Go();
}
public class Car:IVehicle
{
    public void Go()
    {
        Console.WriteLine("Drive");
    }
}

public class SuperCar:IVehicle
{
    public void Go()
    {
        Console.WriteLine("Drive fast!!");
    }
}

IVehicle car = new Car();
car.Go(); //output Drive
car = new SuperCar();
car.Go(); //output Drive fast!!

Upvotes: 1

Binary Worrier
Binary Worrier

Reputation: 51739

The purpose of interfaces is to allow you to use two different classes as if they were the same type. This is invaluable when it comes to separation of concerns.

e.g. I can write a method that reads data from an IDataReader. My method doesn't need to know (or care) if that's a SqlDataReader, and OdbcDataReader or an OracleDataReader.

private void ReadData(IDataReader reader)
{
....
}

Now, lets say I need that method to process data coming from a non-standard data file. I can write my own object that implements IDataReader that knows how to read that file, and my method again, neither knows nor cares how that IDataReader is implemented, only that it is passed an object that implements IDataReader.

Hope this helps.

Upvotes: 6

RobertPitt
RobertPitt

Reputation: 57278

Well interfaces are not meant to be used with just one class, they are used accross many classes to make sure that they contain a set of methods.

a good way to visualize it is to think about driver abstraction, being able to run 1 query that can be interoperated by several different database servers.

interface DatabaseDriver
{
     public function connect(ConnectionDetails $details){}
     public function disconnect(){}
     public function query(Query $query){}
     public function prepareQuery(SQLQuery $query){}
}

and then your actual drivers would use the interface so that the database object can be assured that that the selected driver is able to perform the tasks required.

class MySqlDriver extends Database implements DatabaseDriver{}
class AccessDriver extends Database implements DatabaseDriver{}
class MsSqlDriver extends Database implements DatabaseDriver{}

hope this helps.

Note: Code in PHP

Upvotes: 0

The Muffin Man
The Muffin Man

Reputation: 20004

You've provided a very basic example, which is probably why you're having trouble understand why. Examine something like this:

public interface IDbColumn
    {
        int domainID { get; set; }

    }

    public static IEnumerable<T> GetDataByDomain<T>(
        IQueryable<T> src) where T:IDbColumn
    {
        string url = HttpContext.Current.Request.Url.Host;
        int i = url == "localhost" ? 1 : 2;

        return src.Where(x => x.domainID == i|| x.domainID == 3);
    }

domainID is a physical column in every table that will reference this method, but since the table type isn't known yet there's no way to have access to that variable without an interface.

Upvotes: 1

Jayy
Jayy

Reputation: 14808

Say you have three classes, A, B, C.

A needs to accept an argument. Either B or C can be passed through.

The best way to do this is create an interface that B and C share

Upvotes: 0

SLaks
SLaks

Reputation: 888233

You can write multiple classes that implement an interface, then put any of them in a variable of the interface type.
This allows you to swap implementations at runtime.

It can also be useful to have a List<ISomeInterface> holding different implementations.

Upvotes: 5

Related Questions