Object-oriented differences between C++ and Java

 

Defining and using classes

C++ term

Java term

class

class

member function

instance method

member variable

instance variable

base class

superclass

derived class

subclass

public class identifier

{

    statements;

}

Notes:

  1. There is no semicolon after the closing brace of a Java class definition

  2. Only comments and import statements (to be covered shortly) can be coded outside of a class

  3. In Java, global variables are not allowed

C++

Java

 
  private:
    int quantity;
    double price;
 
  
  
    private int quantity;
    private double price;
  

Access Modifier

Means the feature may be accessed from...

public

any other class

protected

a derived class or any class within the same package

package (default)

any class within the same package

private

only within the class

public static final double TAX_RATE = .06;
public static int transCount;

specifies that TAX_RATE and transCount are class features and not part of any object (instance) of the class. Changes made to a class variable will be seen by all objects of the class.

C++

Java

    Part x;

    Part x = new Part();

Notes:

  1. The new operator obtains memory space for the Part object
  1. Coding Part() calls the default Part constructor
  1. In Java, x is an "object reference". It will contain the address of the Part object (point to it in memory).

x

memory address

->

Part object

For example, if x is a Part object object and addOne() is an instance method of the Part class requiring no parameters, the call expression would be coded as follows:

x.addOne()

For example, to access the public static constant PI of the Math class, the following expression would be coded:

Math.PI

 

Overloading class and instance methods

For example, the following are overloaded methods for calculating sales tax:

public double sTax(double amount, double rate) {
  return amount * rate;
}
  
public double sTax(double amount, int rate) {
  return amount * (rate / 100.0);
}

Notes:

  1. The first method accepts two double parameters (amount and rate) and could be called by the following expression (if order is the object reference):

    order.sTax(19.95, .06)

    Although two literals are shown here, it is more typical to pass variables.

  2. The second method accepts one double parameter (amount) and one int parameter (rate). The literal  100.0  is used in the second method to force conversion of the division result to double. It could be called by the following expression (if order is the object reference):

    order.sTax(19.95, 6)

    Again, two literals are shown so you can clearly see the data types being passed.

order.sTax(19.95, taxRate)

The compiler converts long to double and the first method will be called! To avoid ambiguous calls, always match data types exactly when calling a method.

public Customer() {
    acctNo = 0;
    balance = 0;
    credLimit = 1000;
}
  
public Customer(int iAcctNo, double iBal, double iLimit) {
    acctNo = iAcctNo;
    balance = iBal;
    credLimit = iLimit;
}

Notes:

  1. The first overloaded constructor overrides (replaces) the default constructor. To use this constructor, a programmer might code:

        Customer cust1 = new Customer();

  2. The second overloaded constructor accepts three parameters. A programmer might use this constructor by coding:

        Customer cust2 = new Customer(3440, 0, 5000);

 

Inheritance

public class Customer extends Person

public class Customer extends Person implements Address

 

Overriding class and instance methods

Example:

public class App {

  public static void main(String[] args) {
    App theApp = new AppExtension();
    theApp.iLike();
  }

  public void iLike() {
    System.out.println("I like Paris in the spring time");
  }
}

class AppExtension extends App {
  public void iLike() {
    super.iLike();
    System.out.println("I like Paris in the fall");
  }
}

Notes:

  1. The iLike() method of the AppExtension class overrides the iLike() method of the App class

  2. If the argument list was different, it would constitute an overload instead of an override

  3. If the access modifier was anything less than public, a compile error would occur

  4. If the return type was different, a compile error would occur

  5. To call (unhide) the superclass method, the super reserved word is used

  6. An object reference may be used to point to a subclass object. In this example, an App object reference is used to point to an AppExtension object.

 

Inner classes

Example:

public class App {

  // An instance variable

  private int y;

  // A meaningless inner class...

  public class Inner {
    public void showY() {
      System.out.println("y is " + y);
    }
  }

  // The starting point for the application

  public static void main(String[] args) {

    // Create an object of the inner class and call its showY() method

    App.Inner in = new App().new Inner();
    in.showY();
  }
}

Note: When an inner class is non-static, as in this example, an outer class object must be instantiated in order to reference it.

Example:

public class App {

  // A static class variable

  private static int y;

  // A meaningless, static inner class...

  public static class Inner {
    public static void showY() {
      System.out.println("y is " + y);
    }
  }

  // The starting point for the application

  public static void main(String[] args) {

    // Call the static method of the inner class

    Inner.showY();
  }
}

Example:

public class App {

  private int x = 1;

  public static void main(String[] args) {
    App theApp = new App();
    theApp.showTotal(2);
  }

  public void showTotal(final int y) {

    final int z = 3;

    class Inner{
      public void show() {
        System.out.println("x + y + z = " + (x + y + z));
      }
    }

    Inner theShowTotal = new Inner();
    theShowTotal.show();

  }
}

Note: If y and z aren't final, this code will not compile.