---
title: "What is an interface class? Explain with example."  
description: "What is an interface class? Explain with example."  
author: "Ravi Vishwakarma"  
published: 2021-11-29  
updated: 2023-06-06  
canonical: https://www.mindstick.com/interview/33853/what-is-an-interface-class-explain-with-example  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 4 minutes  

---

# What is an interface class? Explain with example.

> An Interface is an abstract class which has only public abstract methods, and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.
>
> ```
> using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Hello{ interface IListOfTuto // interface {  void SetTutorial(int pID, string pName);  String GetTutorial(); } class HelloWorld : IListOfTuto  // class  {  protected int TutorialID;  protected string TutorialName;  public void SetTutorial(int pID, string pName)  {   TutorialID = pID;   TutorialName = pName;  }  public String GetTutorial()  {   return TutorialName;  }  static void Main(string[] args)  {   HelloWorld pTutor = new HelloWorld();   pTutor.SetTutorial(1,".Net by mindstick.com");   Console.WriteLine(pTutor.GetTutorial());   Console.ReadKey();  } }}
> ```

## Answers

### Answer by Aryan Kumar

An interface class is a special type of class that only defines the methods that a class must implement. It cannot contain any data members or constructors. Interfaces are used to define the behavior of a class, without specifying how that behavior is implemented.

Here is an example of an interface class in C#:

Code snippet

```plaintext
public interface IAnimal
{
    void Speak();
    void Move();
}
```

This interface defines two methods, Speak() and Move(). Any class that implements this interface must provide implementations for both of these methods.

Here is an example of a class that implements the IAnimal interface:

Code snippet

```plaintext
public class Dog : IAnimal
{
    public void Speak()
    {
        Console.WriteLine("Woof!");
    }

    public void Move()
    {
        Console.WriteLine("I'm walking!");
    }
}
```

This class implements the IAnimal interface by providing implementations for the Speak() and Move() methods.

Interfaces are a powerful tool that can be used to decouple the design of a class from its implementation. This can make code more modular and easier to maintain.

Here are some of the benefits of using interface classes:

- **Decoupling:** Interfaces decouple the design of a class from its implementation. This means that the interface can be changed without affecting the classes that implement it.
- **Reusability:** Interfaces can be reused by multiple classes. This can save time and effort in development.
- **Polymorphism:** Interfaces can be used to achieve polymorphism. This means that objects of different types can be treated as if they were the same type.

Interfaces are a valuable tool for any developer. They can be used to improve the design, reusability, and performance of code.

### Answer by Sanjay Goenka

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

An interface is similar to a class in the following ways −

- An interface can contain any number of methods.
- An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
- The byte code of an interface appears in a .class file.
- Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

### Answer by Ravi Vishwakarma

> An Interface is an abstract class which has only public abstract methods, and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.
>
> ```
> using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Hello{ interface IListOfTuto // interface {  void SetTutorial(int pID, string pName);  String GetTutorial(); } class HelloWorld : IListOfTuto  // class  {  protected int TutorialID;  protected string TutorialName;  public void SetTutorial(int pID, string pName)  {   TutorialID = pID;   TutorialName = pName;  }  public String GetTutorial()  {   return TutorialName;  }  static void Main(string[] args)  {   HelloWorld pTutor = new HelloWorld();   pTutor.SetTutorial(1,".Net by mindstick.com");   Console.WriteLine(pTutor.GetTutorial());   Console.ReadKey();  } }}
> ```


---

Original Source: https://www.mindstick.com/interview/33853/what-is-an-interface-class-explain-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
