Return to lecture notes index

15-100 Lecture 14 (Wednesday, October 4, 2006)

Today's Quiz

  // 1. Create a class called Rectangle. 

class Rectangle {

  // 2. It should maintain a length and width (our choice of numerical type)
  private double length;
  private double width;


  // 3. The length and width should be initialized when the Rectangle is allocated
  public Rectangle (double length, double width) {
    this.length = length;
    this.width = width;
  }

  
  // 4. It should also have methods that return the value of the length and width
  
  public double getLength() {
    return length;
  }
  
  public double getWidth() {
    return width;
  }
   

  // 5. It should have a method that changes the width (to something provided via an argument)
  public void setWidth(double newWidth) {
    width = newWidth;
  }


  // 7. Keep in mind public and private stuff

}
  

Interfaces

So far, we've made use of "class reference variables". These reference variables identify an object -- and are limited to identifying objects defined by a specific class. We can also make use of "interface reference variables". These variables are limited to containing references to objects that implement a specific interface, regardless of the class.

So, consider the following:


  CalculatorInterface c = new SimpleCalculator();
  

Notice that, above, we have a "CalculatorInterface" reference identifying a SimpleCalculator. This is useful because it allows us to write code that manipulates Objects, without unnecessarily limiting their types. In the case above, we can implement a whole bunch of Calculators by referring to them as CalculatorInterface without restricting their methods just to a single class object. In this case, we can create a new calculator like a financial calculator which will implement a whole new range of methods but still guarantee the interface methods. Let's go ahead and write the financial calculator and took a more in depth look at this later.

Extends

Now we want to write a new calculator to do some basic financial calculations, however we've already written a simple calculator that can perform all the basic operations that we will still need in our financial calculator as well. There's a way we can implement this fairly easily.

  class FinancialCalculator extends SimpleCalculator {

  public static void main (String[] args) {
   
     FinancialCalculator sc = new FinancialCalculator();
     
     sc.setNumber (10);
     sc.requestOperation('+');
     sc.setNumber (5);
     System.out.println (sc.requestOperation('='));
   
    }
  }
  

Notice that we can still call all the methods we wrote in SimpleCalculator. This is because we added those extra two words in the class definition "extends SimpleCalculator" This means that FinancialCalculator is also a SimpleCalculator and has inherited all of the simple calculator methods.

Let's go ahead an add an addTax method in the FinancialCalculator:

  class FinancialCalculator extends SimpleCalculator {

  public double addTax (double taxRate) {
  
    accumulator = accumulator * (1.0 + taxRate);
  
    return accumulator;
  }


  public static void main (String[] args) {  
     FinancialCalculator sc = new FinancialCalculator();
     
     sc.setNumber (10);
     sc.requestOperation('+');
     sc.setNumber (5);
     System.out.println (sc.requestOperation('='));
     
     sc.addTax (0.05);
     System.out.println (sc.requestOperation('='));
     
   
    }



  }
  

CalculatorInterface vs SimpleCalculator vs FinancialCalculator

So we learned earlier that we can create a FinancialCalculator or SimpleCalculator and store it with a CalculatorInterface reference variable. We can do the same thing with FinancialCalculator. This is because FinancialCalculator also inherited all the CalculatorInterface required methods from SimpleCalculator. Similarly we can also make a FinancialCalculator and store it with a SimpleCalculator reference variable. Here they are in code:


  CalculatorInterface c1 = new SimpleCalculator();
  SimpleCalculator c2 = new SimpleCalculator();
  CalculatorInterface fc1 = new FinancialCalculator();
  SimpleCalculator fc2 = new FinancialCalculator();
  FinancialCalculator fc3 = new FinancialCalculator();
  

Now let's take a look at the operations we can perform with each of these calculators. First off, let's take a look at the simple calculators. Well c2 is just like what we implemented in the main method of our original simple calculator class, and it can perform all the simple calculator operations. c2, however, is referenced as a CalculatorInterace and so the only methods it is guaranteed to have are those specified by the interface (getValue(), setNumber(), and requestOperation()). In other words you can not call compute() on c2.

fc1, fc2, and fc3 act similarly. fc3 can perform all the operations including addTax(). fc2, however, can only implement all the methods specified by SimpleCalculator and fc1 only those in the interface.