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);
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.