What is an Interface Class?

Interfaces are used along with classes to define what is known as a contract. A contract is an agreement on what the class will provide to an application.

An interface declares the properties and methods. It is up to the class to define exactly what the method will do.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DemoApplication

{

interface NewInterface

{

void SetTutorial(int pID, string pName);

String GetTutorial();

}

 

class NewTutorial : NewInterface

{

protected int TutorialID;

protected string TutorialName;

 

public void SetTutorial(int pID, string pName)

{

TutorialID = pID;

TutorialName = pName;gur

}

 

public String GetTutorial()

{

return TutorialName;

}

 

static void Main(string[] args)

{

NewTutorial pTutor = new NewTutorial ();

 

pTutor.SetTutorial(1,”Print .Net”);

 

Console.WriteLine(pTutor.GetTutorial());

 

Console.ReadKey();

}

}

}

Code Explanation:-

  1. We first define an interface called “NwInterface.” Note that the keyword “interface” is used to define an interface.
  2. Next, we are defining the methods that will be used by our interface. In this case, we are defining the same methods which are used in all of earlier examples. Note that an interface just declares the methods. It does not define the code in them.
  3. We then make our NewTutorial class extend the interface. Here is where we write the code that defines the various methods declared in the interface. This sort of coding achieves the following
  • It ensures that the class, NewTutorial, only adds the code which is necessary for the methods of “SetTutorial” and “GetTutorial” and nothing else.
  • It also ensures that the interface behaves like a contract. The class has to abide by the contract. So if the contract says that it should have two methods called “SetTutorial” and “GetTutorial,” then that is how it should be.

Summary

An interface defines a contract which the class will comply with. The interface defines what are the operations that the class can perform.

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close