problem with accessing cub classes of a super class.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GesterX
    New Member
    • Nov 2007
    • 19

    #1

    problem with accessing cub classes of a super class.

    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)

    Code:
    				if (getBoat(i).getType() == "cruiser"){
    					Boat v = getBoat(i);
    					return v;
    					finished = true;
    				}
    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
  • GesterX
    New Member
    • Nov 2007
    • 19

    #2
    EDIT - Okay I thought I fixed it but I didn't.
    Help still apreciated

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      You need to cast from Boat to the subtype:

      [CODE=Java]import java.util.*;

      class Boat {}
      class Cruiser extends Boat {}
      class SpeedBoat extends Boat {}
      class Jetski extends Boat {}


      public class Test {
      public static void main(String[] args) {
      Test app = new Test();
      Jetski craft = app.getFirstJet ski();
      System.out.prin tln("craft != null: " + (craft != null));
      }

      List<Boat> boats = new ArrayList<Boat> ();

      Test() { //some sample data
      boats.add(new Cruiser());
      boats.add(new SpeedBoat());
      boats.add(new Jetski());
      }

      Jetski getFirstJetski( ) {
      for (Boat boat : boats) {
      if (boat instanceof Jetski) {
      return (Jetski) boat; //cast
      }
      }
      return null;
      }
      }[/CODE]

      Comment

      • GesterX
        New Member
        • Nov 2007
        • 19

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

        Cheers

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          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.

          Comment

          Working...