Compare the title\'s of two objects (this.title and other.title) from a class wi
ID: 3623038 • Letter: C
Question
Compare the title's of two objects (this.title and other.title) from a class with the compareTo() method of the object superclass. How would one write a method called compareTo() which would return an integer using the super class's method of compareTo()? I am having a problem calling super.compareTo() from the object super class. For example,
public int compareTo(item other){
int result = this.title.super.compareTo(other.title);
return result;
}
This obviously doesn't work, but is the general idea of what I'm trying to do.
Explanation / Answer
It is not very clear what type(class) is title. It is reasonable to assume your title member is of type String. There is no need to call super class compareTo(). you just say int result = this.title.compareTo(other.title); super is used for two cases in Java 1) If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. 2) in Subclass Constructors for details see http://download.oracle.com/javase/tutorial/java/IandI/super.html
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.