File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Inheritance & Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance & Polymorphism" Watch "Inheritance & Polymorphism" New topic

Inheritance & Polymorphism

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I created the following program, but am having difficulties. Most notably with the compareTo() method I have to implement. Also when I have to call a superclass. I commented out problems throughout the program. Any help would be great! (it's kind of long).
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

In your compareTo() method, the "can't find symbol" error happens because "other", correctly, is an Object reference, and Object doesn't have a "name" member. Since "name" is a member of Threat, you need to case "other" to type Threat before "name" is visible:

compare = other).name);

Now, there's going to be another problem in this method: the compiler is going to complain that if "compare" is zero, the method doesn't return a value. Regardless of whether you think this is possible or not, the compiler will think it is, so you have to return a value no matter what. The easiest thing to do would just be to remove that final "if" and return "compare" directly.

If it were me, I'd probably write compareTo() like this:

Notice that by making it shorter and removing unnecessary logic, it becomes so clear and readable that comments aren't even needed. Also note that if "other" isn't a Threat, then you'll get a ClassCastException. The Javadocs for Comparable explicitly say that this is OK, since it't not really meaningful to impose an ordering on Threats vs. non-Threats.

Now, as to your other questions about the constructor parameters: it depends on what you want to do, really. I've certainly seen plenty of code where a subclass constructor provides default parameters to a superclass constructor. Your default values may well be hard-coded, here based on some notion of how severe a thread "malware" is, for example. So you probably don't want to use "1" everywhere; you probably want to use values that reflect the actual relative threats.

[Jess in Action][AskingGoodQuestions]
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Thanks, that was very helpful. Although i'm still confused on how to make it work in the malware class. In the threat class the compareTo() method will compare by names. In the malware class it's supposed to be overidden, And if both objects are malware, compare them by severity, then by name (last) if severity matches. Any thoughts on that situation? As for the severity when calling the super class, I just initialized it to 1 to make sure the program works, I think i'll leave it for now until I solve the compareTo() problem.
I agree. Here's the link:
subject: Inheritance & Polymorphism
jQuery in Action, 3rd edition