Wednesday, October 16, 2019

Java to C# lingo volume 42: Superclass is Base class

https://www.tutorialspoint.com/equivalent-of-java-super-keyword-in-chash


For super keyword in Java, we have the base keyword in C#.
Super keyword in Java refers immediate parent class instance. It is used to differentiate the members of superclass from the members of subclass, if they have same names. It is used to invoke the superclass constructor from subclass.
C# base keyword is used to access the constructors and methods of base class. Use it within instance method, constructor, etc.
Let us see an example of C# base.

Example

using System;  
public class Animal {  
   public string repColor = "brown";  
}  
public class Reptile: Animal {  
   string repColor = "green";  
   public void display() {  
      Console.WriteLine("Color: "+base.repColor);  
      Console.WriteLine("Color: "+repColor);  
   }     
}  
public class Demo {  
   public static void Main() {  
      Reptile rep = new Reptile();  
      rep.display();  
   }  
}  

Output

Color: brown
Color: green

No comments: