C# Inheritance and Polymorphism



In this C# tutorial, you will learn by code

Inheritance 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpCode
{
 public class Tutorial
 {
  protected int TutorialID; 
  protected string TutorialName;
  
  public void SetTutorial(int pID,string pName) 
  {
   TutorialID=pID;
   TutorialName=pName;
  }
  
  public String GetTutorial()
  {
   return TutorialName;
  }
 }
  public class CsharpCodeTutorial:Tutorial
  {
   public void RenameTutorial(String pNewName)
   {
    TutorialName=pNewName;
   }
  
  static void Main(string[] args) 
  {
   CsharpCodeTutorial pTutor=new CsharpCodeTutorial();
   
   pTutor.RenameTutorial(".Net by CsharpCode");
    
   Console.WriteLine(pTutor.GetTutorial());
    
   Console.ReadKey(); 
  }
 }
}
Polymorphism 
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DemoApplication{ class Tutorial {  public int TutorialID;   public string TutorialName;    public void SetTutorial(int pID,string pName)   {   TutorialID=pID;   TutorialName=pName;  }  public void SetTutorial(string pName)   {   TutorialName=pName;  }  public String GetTutorial()  {   return TutorialName;  }    static void Main(string[] args)   {   Tutorial pTutor=new Tutorial();      pTutor.SetTutorial(1,"First Tutorial");   Console.WriteLine(pTutor.GetTutorial());      pTutor.SetTutorial("Second Tutorial");   Console.WriteLine(pTutor.GetTutorial());       Console.ReadKey();   } }}

0 Comment's

Comment Form