C# | Inheritance in interfaces
C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.
Important Points:
- If a class implements an interface, then it is necessary to implement all the method that defined by that interface including the base interface methods. Otherwise, the compiler throws an error.
- If both derived interface and base interface declares the same member then the base interface member name is hidden by the derived interface member name.
Syntax:
// declaring an interface
access_modifier interface interface_name
{
// Your code
}
// inheriting the interface
access_modifier interface interface_name : interface_name
{
// Your code
}
Example 1:
CSharp
using System;
public interface A {
void mymethod1();
void mymethod2();
}
public interface B : A {
void mymethod3();
}
class Geeks : B
{
public void mymethod1()
{
Console.WriteLine( "Implement method 1" );
}
public void mymethod2()
{
Console.WriteLine( "Implement method 2" );
}
public void mymethod3()
{
Console.WriteLine( "Implement method 3" );
}
}
class GFG {
static void Main(String []args)
{
Geeks obj = new Geeks();
obj.mymethod1();
obj.mymethod2();
obj.mymethod3();
}
}
|
Output:
Implement method 1
Implement method 2
Implement method 3
Example 2:
CSharp
using System;
public interface Votes
{
void vote_no( int a);
}
public interface Details : Votes
{
void detailsofauthor( string n, int p);
}
class TechinalScriptWriter : Details
{
public void vote_no( int a)
{
Console.WriteLine( "Total number of votes is: {0}" , a);
}
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);
}
}
class GFG {
static void Main()
{
TechinalScriptWriter obj = new TechinalScriptWriter();
obj.vote_no(470045);
obj.detailsofauthor( "Siya" , 98);
}
}
|
Output:
Total number of votes is: 470045
Name of the author is: Siya
Total number of published article is: 98
Last Updated :
15 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...