Support for Object Oriented Programming in Java

This Wiki has a good overview of the major differences between Java and C++.

General Characteristics

  • All structured data types (including arrays) must be objects
  • the primitive scalar types (int, float, double) may or may not be explicitly instantiated as objects
  • In Java 5.0+, all primitive types are implicitly coerced into wrapper classes for use in predefined container classes
  • All objects are heap-dynamic, referenced through reference variables, and most allocated with new
  • No explicit delete - garbage collection is automatic
  • A finalize method is implicitly called before garbage collector (similar to C++'s destructor)
  • Does not support operator overloading, pointers or goto ; C++ does
  • minimal compilation unit is a class ; in C++ it is a file
  • In Java, all parameters are passed by value; C++ supports pass-by-reference and pass-by-value

    Inheritance

  • Single inheritance supported only, but there is an abstract class category that provides some of the benefits of multiple inheritance (interface)
  • An interface can include only method declarations and named constants, e.g.,
    	public interface Comparable {
    		     public int comparedTo (Object b);
    	}
    
    Methods can be final (similar to C++ const)

    Dynamic Binding in Java

  • All messages are dynamically bound to methods unless the method is final (if method cannot be overriden dynamic binding serves no purpose)
  • Static binding is also used if the method is static or private (both of which disallow overriding)
  • Several varieties of nested classes
  • All can be hidden from all classes in their package, except for the nesting class
  • Nested classes can be anonymous
  • A local nested class is defined in a method of its nesting class
  • No access specifier is used

    Evaluation

  • Design decisions to support OOP are similar to C++
  • No support for procedural programming - C++ supports procedures outside classes
  • No parentless classes
  • Dynamic binding is used as normal way to bind method calls to method definitions
  • Uses interfaces to provide a simple form of support for multiple inheritance

    Support for OOP in C#

    General characteristics

  • Support for OOP similar to Java
  • Includes both classes and structs
  • Classes are similar to Java's classes

    Inheritance

  • Uses the syntax of C++ for defining classes
  • A method inherited from parent class can be replaced in the derived class by marking its definition with new
  • The parent class version can still be called explicitly with the
           prefix base: base.Draw() 

    Dynamic binding

  • To allow dynamic binding of method calls to methods, The base class method is marked virtual
  • The corresponding methods in derived classes are marked override
  • Abstract methods are marked abstract and must be implemented in all subclasses
  • All C# classes are ultimately derived from a single root class, Object

    Nested Classes

  • A C# class that is directly nested in a nesting class behaves like a Java static nested class
  • C# does not support nested classes that behave like the non-static classes of Java

    Evaluation

  • C# is the most recently designed C-based object-oriented language
  • The differences between C#'s and Java's support for OOP are relatively minor