Showing posts with label super. Show all posts
Showing posts with label super. Show all posts

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

Tuesday, November 29, 2016

Transitioning from C# to Java: Inheritance

Base classes are called superclasses and accordingly referenced with the keyword super rather than C# base. When declaring derived classes, Java extends (for superclasses) and implements (for interfaces) specify the same inheritance relationships for which C# uses simple colons (:).

C# virtual is missing entirely because all Java methods are virtual unless explicitly declared final