C# | Herencia en interfaces

C# permite al usuario heredar una interfaz en otra interfaz. Cuando una clase implementa la interfaz heredada, debe proporcionar la implementación de todos los miembros que están definidos dentro de la string de herencia de la interfaz.
Puntos importantes:  

  • Si una clase implementa una interfaz, entonces es necesario implementar todos los métodos definidos por esa interfaz, incluidos los métodos de la interfaz base. De lo contrario, el compilador arroja un error.
  • Si tanto la interfaz derivada como la interfaz base declaran el mismo miembro, el nombre del miembro de la interfaz base queda oculto por el nombre del miembro de la interfaz derivada.

Sintaxis:

// declaring an interface
access_modifier interface interface_name 
{
   // Your code
}

// inheriting the interface
access_modifier interface interface_name : interface_name
{
    // Your code
}

Ejemplo 1:

CSharp

// C# program to illustrate the concept
// of inheritance in interface
using System;
 
// declaring an interface
public interface A {
     
    // method of interface
    void mymethod1();
    void mymethod2();
}
 
// The methods of interface A
// is inherited into interface B
public interface B : A {
     
    // method of interface B
    void mymethod3();
}
 
 
// Below class is inheriting
// only interface B
// This class must
// implement both interfaces
class Geeks : B
{
     
    // implementing the method
    // of interface A
    public void mymethod1()
    {
        Console.WriteLine("Implement method 1");
    }
     
    // Implement the method
    // of interface A
    public void mymethod2()
    {
        Console.WriteLine("Implement method 2");
    }
     
    // Implement the method
    // of interface B
    public void mymethod3()
    {
        Console.WriteLine("Implement method 3");
    }
}
 
// Driver Class
class GFG {
 
    // Main method
    static void Main(String []args)
    {
         
        // creating the object
        // class of Geeks
        Geeks obj = new Geeks();
         
        // calling the method
        // using object 'obj'
        obj.mymethod1();
        obj.mymethod2();
        obj.mymethod3();
    }
}
Producción: 

Implement method 1
Implement method 2
Implement method 3

 

Ejemplo 2:

CSharp

// C# program to illustrate the concept
// of inheritance in the interface
using System;
 
// declaring an interface
public interface Votes
{
     
    // method of interface
    void vote_no(int a);
}
 
// The methods of interface Votes
// is inherited into interface Details
public interface Details : Votes
{
     
    // method of interface Details
    void detailsofauthor(string n, int p);
}
 
// Below class is inheriting
// the interface Details
// This class must implement
// the methods of both interface
// i.e. Votes and Details
class TechinalScriptWriter : Details
{
 
    // implementing the method
    // of interface Votes
    public void vote_no(int a)
    {
        Console.WriteLine("Total number of votes is: {0}", a);
    }
     
    // implementing the method
    // of interface Details
    public void detailsofauthor(string n, int p)
    {
        Console.WriteLine("Name of the author is: {0}", n);
         
        Console.WriteLine("Total number of published" +
                                " article is: {0}", p);
    }
}
 
// Driver Class
class GFG {
 
    // Main method
    static void Main()
    {
 
        // Creating the objects of
        // TechinalScriptWriter class
        TechinalScriptWriter obj = new TechinalScriptWriter();
         
        // calling the methods by passing
        // the required values
        obj.vote_no(470045);
        obj.detailsofauthor("Siya", 98);
    }
}
Producción: 

Total number of votes is: 470045
Name of the author is: Siya
Total number of published article is: 98

 

Publicación traducida automáticamente

Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *