C# Program to Implement the Same Method in Multiple Classes - GeeksforGeeks

30 Sep, 2021
C# is a general-purpose programming language it is used to create mobile apps, desktop apps, websites, and games. In C#, an object is a real-world entity. Or in other words, an object is a runtime entity that is created at runtime. It is an instance of a class. In this article, Implement the same method in Multiple Classes The implementation of the same method in multiple classes can be achieved by using Interfaces. Interface in C# is just like the class it also has methods, properties, indexers, and events. But it only contains the declaration of the members and the implementation of its members will be given by the class that implements the interface implicitly or explicitly. Or we can say that all the methods which are declared inside the interface are abstract methods. It is used to achieve multiple inheritances which can’t be achieved by class.
Approach:
Create an Interface and declare Method (i.e. greet)
Now create three classes with names Class1, Class2, and Class3 and define the method greet() in all the classes.
Create the class DemoClass and define the main method.
In the Main method create a reference variable for Interface and initialize the reference variable by objects of class1, class2, and class3 and call the method greet().
Example 1:
C#
// C# program to illustrate the above conceptusing System;  interface Interface{          // Declaring Method    void greet();}  // Creating classes and define the method // greet() in all classes.class Class1 : Interface{    public void greet()    {        Console.WriteLine("Welcome Geeks");    }    }  class Class2 : Interface{    public void greet()    {        Console.WriteLine("Hello Geeks");    }    }  class Class3 : Interface{    public void greet()    {        Console.WriteLine("Bella Ciao Geeks");    }    }  class GFG{      public static void Main(String[] args){          // I is the reference variable of Interface    Interface I;          // Initializing the I with objects of all the    // classes one by one and calling the method greet()    I = new Class1();    I.greet();      I = new Class2();    I.greet();          I = new Class3();    I.greet();}}
Welcome Geeks
Hello Geeks
Bella Ciao Geeks
Example 2:
C#
// C# program to illustrate the above conceptusing System;  // Interfaceinterface Interface{          // Declaring Method     void greet();}  // Creating classes and define the method// greet() in all classes.class Class1 : Interface{    public void greet()    {        Console.WriteLine("Welcome Geeks");    }    }  class Class2 : Interface{    public void greet()    {        Console.WriteLine("Hello Geeks");    }    }  class Class3 : Interface{    public void greet()    {        Console.WriteLine("Bella Ciao Geeks");    }    }  class Class4 : Interface{    public void greet()    {        Console.WriteLine("hola geeks");    }    }  class Class5 : Interface{    public void greet()    {        Console.WriteLine("welcome to geeksforgeeks");    }}  class GFG{      public static void Main(String[] args){          // I is the reference variable of Interface    Interface I;          // Initializing the I with objects of all the    // classes one by one and calling the method greet()    I = new Class1();    I.greet();      I = new Class2();    I.greet();          I = new Class3();    I.greet();        I = new Class4();    I.greet();          I = new Class5();    I.greet();}}
Welcome Geeks
Hello Geeks
Bella Ciao Geeks
hola geeks
welcome to geeksforgeeks
CSharp-programs
Picked
Backtracking
C#
Backtracking
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Tug of War
Find if there is a path of more than k length from a source
Difference between Backtracking and Branch-N-Bound technique
N-Queen Problem | Local Search using Hill climbing with random neighbour
Find shortest safe route in a path with landmines
Difference between Abstract Class and Interface in C#
C# | How to check whether a List contains a specified element
C# | IsNullOrEmpty() Method
String.Split() Method in C# with Examples
C# Dictionary with examples