Advertisement
bero_0401

Member Hiding / Shadowing

Aug 19th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | Source Code | 0 0
  1.  
  2. namespace CsharpCourse
  3. {
  4.     /*
  5.       [ Member Hiding / Shadowing ]
  6.     */
  7.  
  8.     public abstract class animal
  9.     {
  10.         protected string Name;
  11.         public animal()
  12.         {
  13.             Name = string.Empty;
  14.         }
  15.         public void sound()
  16.         {
  17.             Console.WriteLine("The sound of animal\n");
  18.         }
  19.  
  20.  
  21.     }
  22.  
  23.     public class cat : animal // inheritance
  24.     {
  25.         public cat()
  26.         {
  27.             Name = "Basbosa";
  28.         }
  29.         public new void sound() // hide the base method
  30.         {
  31.             Console.WriteLine("Meow\n");
  32.         }
  33.  
  34.     }
  35.  
  36.  
  37.     public class dog : animal
  38.     {
  39.     }
  40.  
  41.  
  42.     internal class Program
  43.     {
  44.         static void Main(string[] args)
  45.         {
  46.  
  47.             animal animal = new cat();
  48.             animal.sound(); // call the method in base class
  49.  
  50.             cat obj = new cat();
  51.             obj.sound();
  52.  
  53.             dog obj2 = new dog();
  54.             obj2.sound();
  55.  
  56.         }
  57.  
  58.  
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement