6.170 / Spring 2001 / Lecture 9 Card Answers

Handout L9-QA

Abstract Data Types


Question: In slide 6, what is real? Does it exist as a data type in Java?
Answer: In Java, there is no built-in type named real. You could imagine that the user may have implemented a class named real and was using that for the components of a Point. However, you could also replace the type real in the slides with float or double and still make the same point. (float and double are two built-in primitive types in Java.)

Question:When overloading, does Java also rely on return types?
Answer:
Quick answer: No, it doesn't.
Longer Answer: First of all let's do a quick overview of overloading:

You are overloading a constructor when you declare more than one constructor, and every constructor has different arguments

ie.

public class MyShoe{

  private int shoeSize;
  private String color;

  //constructor 1
  /**
   * @effects: this.color = Brown;
   *           this.shoeSize = 9;
   */ 
  public MyShoe(){
    this.color = "Brown"
    this.shoeSize = 9;
  }

  //constructor 2 
  /**
   * @effects: this.shoeSize = shoeSize;
   */ 	
  public MyShoe(int shoeSize){
    this.shoeSize = shoeSize;
  }

  // constructor 3
  /**
   * @effects: this.color = color;
   *           this.shoeSize = shoeSize;
   */ 
  public MyShoe(int shoeSize, String color){
    this.color = color;
    this.shoeSize = shoeSize;
  }

} 

Now if you call:

MyShoe shoe = new MyShoe(10);

Java knows that you are calling constructor 2, just by comparing the argument (an int) to the arguments of each constructor. Constructor 2 is the only constructor that takes only an int, so it's the one being called.

You can also overload a method by declaring more than one method with the same name and different arguments.

Now, your question is: does Java rely on return types when overloading? The answer is: no, it only relies on the arguments.

If it did rely on the return types, then you could imagine having 2 methods with the same arguments, but different returns. Then Java wouldn't know which one to call! So it causes a compile-time error.

ie


//BAD BAD BAD

public int myShoeSize(){
  return this.shoeSize;
}

public double myShoeSize(){
  return this.shoeSize/ 10.0;
}

=> compile-time error

Note that Java also complains if you try to overload the throws clause of a method.

Question: so does choose() return an arbitrary element from the Set?
Answer: Yes.

Question: How is the preferred implementation of choose() in IntSet (ie returning the first element always) non-deterministic (assuming you know the implementation)? If I knew the order I inserted into the list, then you can determine exactly what element you will get everytime you call choose.
Answer:
If you know the implementation, then yes, you will know which element you choose and it won't be non-deterministic anymore. However, Professor Guttag used an IntSet and if you don't know anything about its implementation, then IntSet.addElement(i) doesn't guarantee anything about the index of i. In that case, returning the first element is non-deterministic since you don't know what it is.

Question: Why is it so bad that interfaces do not have constructors?
Answer:
If you had the ability to declare a constructor in an interface, then you could ensure that every class that implements that interface can be constructed by the same mechanism. This can be useful when you design your ADTs and also if you want to enforce regularity in your code.

Question: When you say the representation is exposed when using mutable objects in an immutable collection, whose representation is exposed?
Answer: The representation of the immutable collection is exposed.

Question: Not knowing the representation of an ADT might prevent you from improving the efficiency (time or space-wise) of the program.
Answer: Yes. But designing ADTs correctly will provide you with good specifications, which will help you ensure the correctness of your code.
Then if you really want, you can look at the ADT's implementation (if you have access to it!) and optimize your code. Just remember that having a non-optimized but well specified ADT is more important than having an under-specified optimized ADT.


Back to Index Card Answers.
Back to the 6.170 home page.
For problems or questions regarding this page, contact: 6.170-webmaster@mit.edu.