C# Program to Implement the Same Method in Multiple Classes
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- 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().
Welcome Geeks Hello Geeks Bella Ciao Geeks
Welcome Geeks Hello Geeks Bella Ciao Geeks hola geeks welcome to geeksforgeeks