5 DIFFERENCES BETWEEN C# AND JAVA: METHODS

5 DIFFERENCES BETWEEN C# AND JAVA: METHODS

Date:
Genres:One of the biggest difference between C# and Java is how they handle methods. In both languages,…
One of the biggest difference between C# and Java is how they handle methods. In both languages, methods are the building blocks of functional code: they define the variables, specify the operations carried out on those variables, and can return values to be used in other methods. Although the creators of the Java language followed many of the concepts of C-based languages, their development of methods allowed for fewer restrictions and more flexibility.

#1: VIRTUAL METHODS

C#: Methods are non-virtual by default
Java: Methods are virtual by default.
A virtual method allows any class that inherits that method to override its functions. In Java, all methods are virtual by default. This default setting prevents the insertion of an unrelated method with the same name in an inherited class.
In C#, the developer must specifically designate a virtual method with the “virtual” keyword:
public class Dimensions
{
public const double pi = Math.PI;
protected double x, y;
public Dimensions()
{
}
public Dimensions (double x, double y)
{
this.x = x;
this.y = y;
}

public virtual double Area()
{
return x*y;
}
}
In order to use the virtual method in an inherited class, the method must include the “override” keyword:
public class Circle: Dimensions
{
public Circle(double r): base(r, 0)
{
}

public override double Area()
{
return pi * x * x;
}
}

#2: GENERATOR METHODS

C#: Supports generator methods
Java: No support for generator methods
Generator methods allow developers to control how a loop handles each iteration. Instead of creating an array, a generator function returns each value of the iteration as the loop processes the code. Java does not have a built-in capability to handle generator functions, and the code to create these functions in Java is often bulky and complicated. C# uses the “IEnumerable” interface to create generator functions and the “yield” command to return the values:
public class TexasCityCollection : IEnumerable<string> {
public IEnumerator<string> GetEnumerator() {
yield return "Houston";
yield return "Auston";
yield return "Dallas";
yield return “San Antonio”;
}
}

#3: PARTIAL METHODS

C#: Supports partial methods with restrictions
Java: No support for partial methods
Just as we saw in the previous lesson regarding partial classes, Java does not support partial methods. In C#, partial methods are allowed, but must meet certain restrictions:
·    The signatures in each part of the partial method must match.
·    The partial method must return void.
·    The partial method is “private” by default and will not accept any modifiers.

#4: EXTENSION METHODS

C#: Supports extension methods
Java: No support for extension methods
Extension methods allow developers to include methods to their current types without the need to create a new type or modify the existing type. The current Java language does not support extension methods, but the feature is under consideration for future versions.

#5: CONDITIONAL METHODS

C#: Supports conditional methods
Java: Limited support for conditional methods
The C# compiler allows for conditional compiling: developers can enter parameters during compilation to determine which methods the program will use. Java also allows for conditional compiling, but this practice is typically used in the debugging process.

0/Post a Comment/Comments

Previous Post Next Post
PopAds.net - The Best Popunder Adnetwork