Open In App

C# Program to Implement the Same Method in Multiple Classes

Improve
Improve
Like Article
Like
Save
Share
Report

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 concept
using 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();
}
}


Output

Welcome Geeks
Hello Geeks
Bella Ciao Geeks

Example 2:

C#




// C# program to illustrate the above concept
using System;
  
// Interface
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 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();
}
}


Output

Welcome Geeks
Hello Geeks
Bella Ciao Geeks
hola geeks
welcome to geeksforgeeks


Last Updated : 30 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads