5 DIFFERENCES BETWEEN C# AND JAVA: OBJECTS AND CLASSES

5 DIFFERENCES BETWEEN C# AND JAVA: OBJECTS AND CLASSES

Date:
Genres:One of the most important aspects of C-derived languages is object orientation. Objects and clas…
One of the most important aspects of C-derived languages is object orientation. Objects and classes allow programs to specify methods and variables in one portion of code and use them again wherever necessary. While the basic structures of class construction remain consistent between C# and Java, some subtle differences my cause problems for developers unaccustomed to the idiosyncrasies between the two languages.

#1: INSTANCE-LEVEL INNER CLASSES

C#: Work-around support Instance-level inner classes
Java: Support for Instance-level inner classes
An inner class (also called a “nested class”) is declared entirely inside another class or interface. Although both languages support inner classes at the Class level, only Java supports these inner classes at the instance level without the need to pass around the outer object instance. Java handles the instance-level inner class with an “outer this pointer”.

#2: PARTIAL CLASSES

C#: Supports partial classes
Java: No support for partial classes
A “partial class” is a class whose methods and variables are parceled out into multiple files. When the files are compiled, the class reassembles itself into the full class definition. While the C# 2.0 compiler (and other OOP compilers) allows for class files to merge at compile time, the Java compiler does not. In Java, each class must be in its own specific source code file.

#3: ANONYMOUS CLASSES

C#: Supports statement-level anonymous classes
Java: Supports implicit anonymous classes
An anonymous class is just that: a class without a name. Developers often define anonymous classes within a method to build simple delegate callback objects, such as those used in listener methods. Java treats anonymous classes as implicit, but C# code must defined the anonymous class at the statement level.

#4: PROPERTIES

C#: Supports properties
Java: Does not support properties
A property uses the tools of a method while holding a value like a variable:
// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
Although other Java-related languages and toolsets (e.g. JavaBeans and JavaScript) support similar ways of defining a property, Java does not.

#5: EVENTS

C#: Supports events
Java: Work-around support for events
An event is a way that a class can notify its clients that an action has occurred that affects some method or variable within the object.  Although Java does not support the “event” keyword for this specific purpose, Java developers can create a class that has much of the same behavior as an event.

HONORABLE MENTIONS

Operator Overloading
C#: Supports
Java: Does not support
According to the Java FAQ, Java does not support operator overloading “because C++ has proven by example that operator overloading makes code almost impossible to maintain”.
Indexers
C#: Supports
Java: Does not support
Indexers allow class instances to be indexed and counted in ways similar to arrays for variables. Class instances in Java can still be indexed, but the “get” and “set” methods must be specified as variables.
Conversions
C#: Supports
Java: Does not support
C# allows both implicit and explicit conversions from one data type to another. Java requires that the user specifically state the conversion method.

0/Post a Comment/Comments

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