By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

problem with accessing cub classes of a super class.

P: 19
Hi guys, this has been bugging me for a while and I'd appreciate any help anyone could offer.

Basically I am running a boating simulation and I have all of the "harder" parts done but one error is holding me back from compiling my code.

I won't go into detail about the simulation but basically I have a Boat super class and extended from this are the sub classes; cruiser, speedboat, and jetski.
My problem is when it comes to accessing methods in the cruiser, speedboat, and jetski classes.

For example here is a section of my code (the context is unimportant)

Expand|Select|Wrap|Line Numbers
  1.                 if (getBoat(i).getType() == "cruiser"){
  2.                     Boat v = getBoat(i);
  3.                     return v;
  4.                     finished = true;
  5.                 }
  6.  
The getBoat method expects a boat to be returned but since the Cruiser class extends the Boat class, I thought it would be accpetable to return a cruiser? If I create the variable "v" as a cruiser I still get a type mismatch error.

So my question is how can I get this part of my method to convert or accept the object so that there is no type mismatch.

I'm happy to quickly provide any other information that may help you solve my problem.

Regards
GX
Apr 15 '08 #1
Share this Question
Share on Google+
4 Replies


P: 19
EDIT - Okay I thought I fixed it but I didn't.
Help still apreciated
Apr 15 '08 #2

BigDaddyLH
Expert 100+
P: 1,216
You need to cast from Boat to the subtype:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class Boat {}
  4. class Cruiser extends Boat {}
  5. class SpeedBoat extends Boat {}
  6. class Jetski extends Boat {}
  7.  
  8.  
  9. public class Test {
  10.     public static void main(String[] args) {
  11.         Test app = new Test();
  12.         Jetski craft = app.getFirstJetski();
  13.         System.out.println("craft != null: " + (craft != null));
  14.     }
  15.  
  16.     List<Boat> boats = new ArrayList<Boat>();
  17.  
  18.     Test() { //some sample data
  19.         boats.add(new Cruiser());
  20.         boats.add(new SpeedBoat());
  21.         boats.add(new Jetski());
  22.     }
  23.  
  24.     Jetski getFirstJetski() {
  25.         for (Boat boat : boats) {
  26.             if (boat instanceof  Jetski) {
  27.                 return (Jetski) boat; //cast
  28.             }
  29.         }
  30.         return null;
  31.     }
  32. }
Apr 15 '08 #3

P: 19
Oh I see thankyou very much indeed!
I implemented what you said and saw all of those nasty red X's go away.

Cheers
Apr 15 '08 #4

Expert 100+
P: 849
You should also be getting an error about the statement finished=true; being unreachable. This occurs because return immediately exits the function, so statements after it in the same block are not executed. Ever.
Apr 16 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.