In Chapter 2:
Advanced : Java vs C/C++ we compared the basic syntax of Java
and to the syntax of these two languages. Here we list the differences
in class, method and object approaches between Java and C++.
Unlike C++, Java has:
- No multiple inheritance - use interfaces (discussed
in Chapter 4)
instead.
- Different access scheme - the private,
protected,
public
and default access modifiers (access will be discussed in Chapter
5) function somewhat differently than in C++.
- No method pointers - although Java reflection classes,
discussed later, allow access to methods, they are not intended
for use as callbacks the way that method pointers are used in
C/C++. Instead, the abstract methods in interfaces,
discussed later, are the preferred mechanism for callback tasks.
- Fixed lengths of method argument lists - there is no
option to vary the number of arguments in a method call, such
as in C/C++ where a method invocation can leave out some arguments
and their values are replaced by default values. Instead Java
offers method overloading.
- No void
for empty method argument list - an empty argument list for
a method is just left blank, as in method().
- java.lang.Object
- all Java classes inherit Object
(see Chapter
5). It provides a general reference type to any Java object
in a similar way that void
* pointers are used in C++.
- String
objects - these do not allow direct access to their internal
char arrays.
In C/C++, there is no similar string class built into the core
language. Instead character arrays are used. In Java you can also
use char
arrays, but usually String
objects are more convenient since they come with a large array
of useful methods.
- No operator overloading - this would be convenient, especially
for implementing complicated mathematical equations, but this
was left out of Java for the sake of simplicity (except for the
"+" append operator for String
objects).
- No templates - also left out for the sake of simpicity.
- "final
static" rather than "const"
- can also use the final
modifier for fields and methods but not in the argument list.
- No destructors in Java - the finalizers are similar but
no control given to the user over when a finalizer will be called.
- Only the new
operator to create objects - no short term local variable
creation of objects as in C++.
- Pure virtual method calls in a constructor - an abstract
method in class A can be invoked in its constructor if the sub-class
B has provided a concrete implementation of that method.
- All methods treated as virtual - that is, unless modified
by final,
a table of methods is always checked to see if the method has
been overridden by a sub-class.
References
& Web Resources
Latest update: Dec.15.2003
|