Previous Table of Contents Next


14.3.10. C++ Features Not Found in Java

Throughout this section, we’ve noted similarities and differences between Java and C++ in footnotes. Java shares enough concepts and features with C++ to make it an easy language for C++ programmers to pick up. There are several features of C++ that have no parallel in Java, however. In general, Java does not adopt those features of C++ that make the language significantly more complicated. These omissions from Java (or simplifications of C++) are described below.

C++ supports “multiple inheritance” of method implementations from more than one superclass at a time. While this seems like a very useful feature, adding it to the language actually turns out to introduce many complexities. The Java language designers chose to avoid the added complexity by using interfaces instead. Thus, a class in Java can only inherit method implementations from a single superclass, but it can inherit method declarations from any number of interfaces. In practice, this is not any particular hardship.

C++ supports (though not yet in a very standardized way) templates that allow you, for example, to implement a Stack class and then instantiate it as Stack<int> or Stack<double> to produce two separate types: a stack of integers and a stack of floating-point values. Java has no such facility. However, the fact that every class in Java is a subclass of Object means that every object can be cast to an instance of Object. Thus, in Java, it is often sufficient to define a data structure (such as a Stack class) that operates on Object values—the objects can be cast back to their actual type whenever necessary.

C++ allows you to define operators that perform arbitrary operations on instances of your classes. In effect, it allows you to extend the syntax of the language. This is a nifty feature, called operator overloading, that makes for very elegant examples. In practice, however, it tends to make code hard to understand. After much debate, the Java language designers decided to omit such “operator overloading” from the language. Note, though, that the use of the + operator for string concatenation in Java is at least reminiscent of operator overloading.

C++ allows you to define “conversion functions” for a class that automatically invokes an appropriate constructor method when a value is assigned to a variable of that class. This is simply a syntactic shortcut (similar to overriding the assignment operator) and is not included in Java.

In C++, objects are by default manipulated by value; you must use & to specify a variable or function argument that is automatically manipulated by reference. In Java, all objects are manipulated by reference, so there is no need for this & syntax.

14.3.11. Summary

This has been a long and detailed section. The following list summarizes the most important points to remember. This summary is not intended to simplify the complex material we’ve covered, but it may allow you to test your comprehension of the material now, and to help jog your memory later:

  A class is a collection of data and methods that operate on that data.
  An object is a particular instance of a class.
  Object members (fields and methods) are accessed with a dot between the object name and the member name.
  Instance (non-static) variables occur in each instance of a class.
  Class (static) variables are associated with the class. There is one copy of a class variable regardless of the number of instances of a class.
  Instance (non-static) methods of a class are passed an implicit this argument that identifies the object being operated on.
  Class (static) methods are not passed a this argument and therefore do not have a current instance of the class that can be used to implicitly refer to instance variables or invoke instance methods.
  Objects are created with the new keyword, which invokes a class constructor method with a list of arguments.
  Objects are not explicitly freed or destroyed in any way. The Java garbage collector automatically reclaims objects that are no longer being used.
  If the first line of a constructor method does not invoke another constructor with a this() call, or a superclass constructor with a super() call, Java automatically inserts a call to the superclass constructor that takes no arguments. This enforces “constructor chaining.”
  If a class does not define a constructor, Java provides a default constructor.
  A class may inherit the non-private methods and variables of another class by “subclassing”—i.e., by declaring that class in its extends clause.
  java.lang.Object is the default superclass for a class. It is the root of the Java class hierarchy and has no superclass itself. All Java classes inherit the methods defined by Object.
  Method overloading is the practice of defining multiple methods which have the same name but have different argument lists.
  Method overriding occurs when a class redefines a method inherited from its superclass.
  Dynamic method lookup ensures that the correct method is invoked for an object, even when the object is an instance of a class that has overridden the method.
  static, private, and final methods cannot be overridden and are not subject to dynamic method lookup. This allows compiler optimizations such as inlining.
  From a subclass, you can explicitly invoke an overridden method of a superclass with the super keyword.
  You can explicitly refer to a shadowed variable with the super keyword.
  Data and methods may be hidden or encapsulated within a class by specifying the private or protected visibility modifiers. Members declared public are visible everywhere. Members with no visibility modifiers are visible only within the package.
  An abstract method has no method body (i.e., no implementation).
  An abstract class contains abstract methods. The methods must be implemented in a subclass before the subclass can be instantiated.
  An interface is a collection of abstract methods and constants (static final variables). Declaring an interface creates a new data type.
  A class implements an interface by declaring the interface in its implements clause and by providing a method body for each of the abstract methods in the interface.


Previous Table of Contents Next