Home : Course Map : Chapter 3 : Java : Supplements :
Classes: Java vs C++
JavaTech
Course Map
Chapter 2

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

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

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.