Advertisement
bero_0401

virtual - protected

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