Equivalent of Java Super Keyword in C#

CsharpProgrammingServer Side Programming

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

 Live Demo

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
raja
Published on 17-Sep-2018 12:36:13
Advertisements