Wednesday, February 18, 2015

C# vs Java : constructors

Both C# and Java allow you to invoke a constructor from another constructor that's in the same class.

Here's what that looks like in C#:
public class Klass {
  public Klass() {
    System.Console.WriteLine("pubic Parent()");
  }

  public Klass(string str) : this() {
    System.Console.WriteLine("pubic Parent(String str)");
    System.Console.WriteLine("\tstr = " + str);
  }

  public static void Main() {
    new Klass("Salut");
  }
}
The syntax looks a little different in Java, though. In particular, the placement of the this() method.
public class Klass {

   public Klass() {
      System.out.println("pubic Parent()");
   }

   public Klass(String str) {
      this();
      System.out.println("pubic Parent(String str)");
      System.out.println("\tstr = " + str);
   }

   public static void main(String... args) {
      new Klass("Salut");
   }
}
See Also: Java's super and this keywords
Whereas Java has the static initializer block...
public class Klass {

   public static String str;

   static {
      str = "Salut";
   }

   public static void main(String... args) {
      System.out.println("Klass.str = " + Klass.str);
   }
}
C# has a static constructor.
public class Klass {
  public static string str;

  static Klass() {
    str = "Salut";
  }

  public static void Main() {
    System.Console.WriteLine("Klass.str = " + Klass.str);
  }
}
Note: The static constructor must be parameterless. You'll get a compiler error otherwise

See Also: Java's static Keyword
Both C# and Java allow you to write classes that inherit from another class.

In C# you use a colon followed by the name of the class your inheriting from.
class Child : Parent {
In Java you use the extends keyword
class Child extends Parent
It should also be noted that what Java calls the superclass C# calls the base class and what Java calls the subclass C# calls the derived class.
Just as you can have a constructor call another constructor, you can also have a constructor that's in the subclass (ie. derived class) invoke a constructor that's in the superclass (ie. base class).

Here's what that looks like in C#
public class Parent {
  public Parent(string str) {
    System.Console.WriteLine("pubic Parent(String str)");
    System.Console.WriteLine("  str = " + str);
  }
}

public class Child : Parent {    
  public Child() : base("Salut") {
    System.Console.WriteLine("pubic Child()");
  }

  public static void Main() {
    new Child();
  }
}
In Java you use the super keyword.
public class Parent {

   public Parent(String str) {
      System.out.println("pubic Parent(String str)");
      System.out.println("\tstr = " + str);
   }
}


public class Child extends Parent {
   public Child() {
      super("Salut");
      System.out.println("public Child()");
   }

   public static void main(String... args) {
      new Child();
   }
}
Both programs output the same thing.
public Parent(String str)
  str = Salut
public Child()

No comments:

Post a Comment