Both C# and Java are object-oriented languages, with some striking differences and similarities in how they implement the object-oriented programming (OOP) model. Table 3.2 compares the Java and C# object-oriented programming models.
One of the most important differences between the OOP model used by Java and that of C# is that in Java all methods are, by default, virtual. This means that the method call is always on the object instantiated. Therefore,
Table 3.2. The Java and C# OOP Models
|
Inheritance | Single inheritance | Single inheritance |
Object hierarchy | Single root. All objects derive from one class (java.lang.Object) except for base types such as int and float. | Single root. Everything derives from one class (System.Object). |
| | Objects are created using the new operator. | Objects are created using the new operator. |
| | Members are accessed using the "." operator. | Members are accessed using the "." operator. |
Polymorphism | Methods are virtual by default. The only way to prevent subclasses from overwriting superclass implementations is by making the superclass method private or making it final. | Methods are nonvirtual by default. You must use the virtual and override keyword combination (on the superclass and subclass, respectively) to initiate polymorphism. |
Equivalent access modifiers | public | Public |
private | Private |
protected | protected internal |
package | Internal |
Default access level | package | private |
Access modifier rules | Subclasses can access only the public and protected members of the superclass. | Subclasses can access only the public and protected members of the superclass. |
| | Subclasses cannot restrict the access of a method inherited from the superclass; a public superclass method cannot be made private in the subclass. | Subclasses can restrict the access modifier of a method on the superclass as long as the subclass is not overriding the method to activate virtual dispatching. |
Static methods | Public and protected static methods can be inherited. Private static methods cannot be inherited.
Virtual dispatching is not activated for static methods.
You can access a static member through a nonstatic reference variable. | Public and protected static methods can be inherited. Private static methods cannot be inherited.
Virtual dispatching is not activated for static methods because the keywords virtual and override are not allowed in a static context.
You cannot access a static method using a nonstatic reference. |
Abstract methods | All nonabstract subclasses of an abstract superclass must have an implementation for all the superclass abstract methods or must be marked as abstract. | All nonabstract subclasses of an abstract superclass must have an implementation for all the superclass abstract methods or must be marked as abstract. |
Interfaces | Classes can implement multiple interfaces, and interfaces can inherit from each other.
An interface must be public if it is to be implemented by a class in a different package.
A class implementing an interface method must declare it public. A class can implement multiple interfaces with common method names. There is no explicit interface declaration, and therefore multiple interfaces share the same implementation for the common method name. | Classes can implement multiple interfaces, and interfaces can inherit from each other.
A class can implement any interface even if they are in different namespaces.
A class implementing an interface method can have the method declared as public or can explicitly declare it using explicit interface declaration. This means that you can implement two different interfaces that have the same method name.
Presence of the as operator, which reduces casting. |
Constructors and destructors | Provides a default no-argument public constructor if the class does not already provide one. Static blocks are used instead of static constructors. Finalizers are like destructors but are not as deterministic as destructors. There is no guarantee as to when they will get called. | Provides a default no-argument public constructor if the class does not already provide one.
Static constructors are used.
C++-like destructors. Destructors can be made only for references types and not for value types, and destructors cannot be overridden. A destructor cannot be explicitly called, because it doesn't make much sense to do so when an object's lifetime is managed by the garbage collector. You are encouraged to use the IDisposable interface for releasing resources instead of releasing them in destructors. |
Miscellaneous features | Inner and anonymous classes. | Inner but no anonymous classes. Support for anonymous methods. |