My thought about C# and Java

I have used and programmed in both languages professionally and academically.

For C#, I prefer VS studio or its light weighted version VS code whereas Java, I prefer Eclipse or NetBean.

These two languages are high level languages object oriented programming languages where it uses Class and Object for organizing code and code reusability/scalability.

These two programming languages are quite similar, in my own opinion it is 88% similar since they operate based on the same concept of OOP e.g Class, Object, A. P.I.E (Abstraction, Polymorphism, Inheritance & Encapsulation), Multithreading, Thread-safe capability, Exception Error Handling, Auto memory management with its memory garbage collector, Communication between multithreading, Primitive types and Reference types, Type safety, access modifier. C# supports both strongly type and weakly type like dynamic (runtime type) whereas Java only supports strongly (statically) type.

In short, they both emphasize SOLID capabilities.

S: Single Responsibility principle:

a class should only have one reason to change or in short, a class should only have single responsibility.

E.g: if an employee class has EmployeeSalaryCalculation() and also SaveEmployeeData(), it breaks this principle since these two actions are two different responsibilities and to satify this principle we should separate it to another class e.g Employee class (salary related etc.) and EmployeeReposity (data/db related etc.)

O: Open/Closed principle:

Open for Extention but Closed for modification. In short, it refers to abstract class and sub class where sub class can derive from abstract class and make its own version (extension) of abstract class method (abstract method) but no modication can be made to the abstract method because abstract method has no implementation (Closed for modification).

L: Liskov principle:

In short, it refers to polymorphism. Poly means many form. it refers to the ability of object to take on many forms. e.g a sub class can be used in place of the parent class and its method can change based on the version of each sub class (object can take on many forms). There is two types of polymorphism. Overloading (compile time polymorphism) and overriding (runtime polymorphism). Overloading e.g method overloading where same method name with different number of arguments can behave diferently based on the number of arguments whereas Overriding e.g several diferent sub class override parent class’s method.

I: Interface Segregation principle:

a client should not be forced to implement interface that they do not need or use. In short, we should always try to break big interface into subtle interface so that way it is easy for a client to only implement interface it needs and also since both Java and C# support multiple interfaces implmentation.

D: Dependency inversion principle:

This principle emphasizes the importane of decoupling high level modules from low level module. high level module should not depend on low level module and vice versa through abstraction and interface. In short, we can accomplish this principle through dependeny injection so instead of having low level class as member of our high level class (hard coupling) we should introduce interface and have interface as a member instead so that way the low level classes that implement the interface can be inject at runtime either through constructor or method that accept the interface type as an argument.

Ok let get back to illustrate that both langauge are quite similar. if you can program in C#, you should definitely be able to program in Java easily.

  • Both support Class and instantiate of the Class aka Object
  • Both do not support multiple inheritance
Java
-------------------------------------
Class MySubClass extends MySuperClass
C#
-------------------------------
Class MySubClass : MySuperClass
  • Both support ability to call super class (base class)constructor
Java
super(args)
C#
base(args)
  • Both support multiple interfaces implementation
Java

class MySubClass implements MyInterface1, MyInterface2
C#
class MySubClass : MyInterface1, MyInterface2
  • Both support the ability to prevent other classes from inheriting from a class
Java
final class Vehicle {
void display() {
System.out.println("This is a vehicle");
}
}

// This will cause a compile-time error
// Error: Cannot inherit from final 'Vehicle'
class Car extends Vehicle {

}
C#
sealed class Vehicle
{
public void Display()
{
Console.WriteLine("This is a vehicle");
}
}

// This will cause a compile-time error
// Error: 'Vehicle' is sealed and cannot be inherited
class Car : Vehicle
{
}
  • Java final field and C# readonly field

Both does the exact same thing: Once assigned, cannot be changed.

  • Java final method and C# sealed override method

Both does exact same thing: it prevents sub class from overriding the super class method.

  • Both support Multithreading through Thread class
Java
a) Create Thread through Extend Thread class
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}

public class Main {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();

MyThread t2 = new MyThread();
t2.start();
}
}

b)Create Thread through Implement Runnable interface
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable running");
}
}

public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
t1.start();

Thread t2 = new Thread(new MyRunnable());
t2.start();
}
}
C#
using System;
using System.Threading;

class Program {
static void Main() {
Thread t1 = new Thread(new ThreadStart(Run));
t1.Start();

Thread t2 = new Thread(new ThreadStart(Run));
t2.Start();
}

static void Run() {
Console.WriteLine("Thread running");
}
}
  • Both support Exception and Error handling through try/catch/finally block
Java
try {
// code that might throw an exception
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Finally block always executes");
}
C#
try {
int result = 10 / 0;
} catch (DivideByZeroException e) {
Console.WriteLine("Cannot divide by zero");
} finally {
Console.WriteLine("Finally block always executes");
}
  • Both support throws exception within a method so as for a calling method to handle/catching the exception and log exception appropriately

Note: there is subtle difference where Java support checked Exception (forced checking exception by the compiler so the calling method has to properly handle it) through throw whereas C# does not have checked Exception.

Java
void readFile() throws IOException {
//code
throw new IOException("File not found");
}
C#
void ReadFile() {
throw new IOException("File not found");
}
  • Both support Encapsulation through access modified e.g private, protected, public etc…

Java Access Modifiers:

ModifierClassPackageSubclassThe rest of the World
private✅ Yes❌ No❌ No❌ No
default (no modifier)✅ Yes✅ Yes❌ No❌ No
protected✅ Yes✅ Yes✅ Yes❌ No
public✅ Yes✅ Yes✅ Yes✅ Yes

C# Access Modifiers:

ModifierClassDerived ClassesSame AssemblyOther Assemblies
private✅ Yes❌ No❌ No❌ No
protected✅ Yes✅ Yes❌ No❌ No
internal✅ Yes❌ No✅ Yes❌ No
protected internal✅ Yes✅ Yes✅ Yes❌ No
public✅ Yes✅ Yes✅ Yes✅ Yes

Note: in C#

class without access modifier = internal class.

class members without access modifier = private field.

  • Both support ThreadSafety

First what is thread safe. Thread safe means to prevent race condition between multiple thread. race codition occures when multiple threads access and update a shared resource at the same time. To prevent race condition in multiple thread, C# use lock object and Java use synchronize object so only one thread can acquired the shared resource and update it once at a time.

  • Both support communication between multiple thread

C# use AutoResetEvent or ManualResetEvent to allow one thread to signal one or more waiting threads that some event has occurred whereas Java uses notify() and notifyAll()

There are a few more but with these it shows that both languages are quite similar so if we can program in C# we can say we can program in Java. Java is an independent platform language and with .net core, it makes c# also an independent platform language.