Saturday, August 27, 2016

C# vs Java : Interfaces

Here's the Java way to declare a class implements an interface
public class MyClass implements MyInterface {
}
Here's the C# way
public class MyClass : MyInterface {
}

Both C# and Java allow you to extend (ie inherit) from another interface:
// Java
public interface MyInterface extends SomeInterface {
}
// C#
public class MyInterface : SomeInterface {
}

C# doesn't allow you to specify access modifiers when declaring methods inside an interface:
public interface MyInterface {

   // compiler error
   public void fooBar();
}
The above will result in a compiler error similar to the following:
The modifier 'public' is not valid for this item
Java, on the other hand, will allow this.

C# only allows you to declare methods inside an interface. Recall that in Java you can declare static fields inside an interface.
public interface MyInterface {
   String str = "Salut le Monde";
}
public class MyClass implements MyInterface {

   public static void main(String... args) {
      System.out.println("Hello World");
      System.out.println(str);
   }
}
Here's what the above would output:
Hello World
Salut le Monde
Check out the post I did on Java's static Keyword

You can even declare an enum inside of an interface in Java:
public interface MyInterface {

   public enum Suit {
      HEARTS, DIAMONDS, SPADES, CLUBS;
   };
}
public class MyClass implements MyInterface {

   public static void main(String... args) {
      System.out.println(Suit.HEARTS);

      // System.out.println(HEARTS); // compiler error
   }
}
Notice that you have to prefix the enum element with the enum's name.

Check out the post I did on Java Enum
Sometimes you'll have a class that implements multiple interfaces and there will be a method that's declared in more than one of those interfaces. For example
// Java
public interface MyInterface {
   public void fooBar();
}

public interface SomeInterface {
   public void fooBar();
}

public class MyClass implements MyInterface, SomeInterface {
   
   public void fooBar() {
      System.out.println("Hello World");
   }
   
   
   public static void main(String... args) {
      new MyClass().fooBar();
   }
}
Notice that when you implement an interface in Java you can only define the method once inside the class.

Check out the post I did on Java's Method Overloading vs Method Overriding

C# has a feature that allows you to explicitly state that x method should be used with instances of y type interface. In other words, you can define the same method more than once inside the class.
interface MyInterface {
   void fooBar();
}
  
interface SomeInterface {
   void fooBar();
}
  
class MyClass : MyInterface, SomeInterface {   

   void MyInterface.fooBar() {
      Console.WriteLine("Hello World!");
   }

   void SomeInterface.fooBar() {
      Console.WriteLine("Salut le Monde");
   }
  
   public static void Main(string[] args) {
      MyClass klass = new MyClass();

      MyInterface english = (MyInterface) klass;
      english.fooBar();
   
      SomeInterface francais = (SomeInterface) klass;
      francais.fooBar();

      // klass.fooBar(); // compiler error
  }
}
Notice that you can no longer access the method through the instantiated object.

No comments:

Post a Comment