C#
C# Inheritance & Polymorphism with Examples
In this tutorial, you will learn- Inheritance Polymorphism What is Inheritance in C#? Inheritance is...
Interfaces are used along with classes to define what is known as a contract. A contract is an agreement on what the class will provide to an application.
An interface declares the properties and methods. It is up to the class to define exactly what the method will do.
Let's look at an example of an interface by changing the classes in our Console application. Note that we will not be running the code because there is nothing that can be run using an interface.
Let's create an interface class. The class will be called "Guru99Interface." Our main class will then extend the defined interface. All the code needs to be written in the Program.cs file.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { interface Guru99Interface { void SetTutorial(int pID, string pName); String GetTutorial(); } class Guru99Tutorial : Guru99Interface { protected int TutorialID; protected string TutorialName; public void SetTutorial(int pID, string pName) { TutorialID = pID; TutorialName = pName; } public String GetTutorial() { return TutorialName; } static void Main(string[] args) { Guru99Tutorial pTutor = new Guru99Tutorial(); pTutor.SetTutorial(1,".Net by Guru99"); Console.WriteLine(pTutor.GetTutorial()); Console.ReadKey(); } } }
Code Explanation:-
Here, we explain the important sections of the code
Summary
In this tutorial, you will learn- Inheritance Polymorphism What is Inheritance in C#? Inheritance is...
What is an Abstract Class in C#? ABSTRACT CLASS can never be instantiated and is marked by the...
What are Data Types in C#? The C# language comes with a set of Basic data types. These data types are...
So far we have seen how to work with C# to create console based applications. But in a real-life...
What is ArrayList in C#? The ArrayList collection is similar to the Arrays data type in C#. The...
What is Hashtable in C#? A hash table is a special collection that is used to store key-value...