• +91 9723535972
  • info@interviewmaterial.com

CSharp Interview Questions and Answers

Question - Define interface class in C#? Explain with an example.

Answer -

An interface class is completely an abstract class that contains abstract methods and properties. By default, the members of the interface class are abstract and public with no fields defined. If you want to access the interface methods then the interface must be implemented by another class using ‘:’ symbol. If you want to define the body of the methods that can only be implemented in the implementing class.

For example:

// Interface
Interface IAnimal {
  void Sound(); // interface method (without body)
}
class Pig : IAnimal   // Pig class "implements" the IAnimal interface
{
  public void Sound()
  {  
Console.WriteLine("The pig says: wee wee"); // The body of Sound() is provided her
  }
}
class Program
{
  static void Main(string[] args)
  {
 Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
  }}

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners