473,661 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with accessing cub classes of a super class.

19 New Member
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
4 1410
GesterX
19 New Member
EDIT - Okay I thought I fixed it but I didn't.
Help still apreciated
Apr 15 '08 #2
BigDaddyLH
1,216 Recognized Expert Top Contributor
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
GesterX
19 New Member
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
Laharl
849 Recognized Expert Contributor
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

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

Similar topics

1
1579
by: Stephane Ninin | last post by:
Hello all, I am trying to play with nested class in a script I am making, and I am not sure I really understand how they work. Here is some code: __all__ =
8
1544
by: Dan Perl | last post by:
Here is a problem I am having trouble with and I hope someone in this group will suggest a solution. First, some code that works. 3 classes that are derived from each other (A->B->C), each one implementing only 2 methods, __init__ and setConfig. ------------------------------------------------------- #!/usr/bin/python class A (object): def __init__(self): super(A, self).__init__() self.x = 0
16
2053
by: Simon Wittber | last post by:
I've noticed that a few ASPN cookbook recipes, which are recent additions, use classic classes. I've also noticed classic classes are used in many places in the standard library. I've been using new-style classes since Python 2.2, and am suprised people are still using the classic classes. Is there a legitimate use for classic classes that I am not aware of?
2
1365
by: Uwe Mayer | last post by:
Hi, I have a diamond-shaped multiple inheritanc chain with new style classes, but super() does not call the parent class correctly: -- snip -- from qtcanvas import * class B2(QCanvasItem): def move(self, x,y):
6
7662
by: nick | last post by:
I have tried finding an answer to this, but most people just explain classes as a more modular way to program. It seems to me that (forgetting OO programming which I don't quite understand) the structures in C are the same as classes in another language such as C++ or Java only missing the ability to make the data private. I've never had this explained sufficiently and would appreciate a good answer. It doesn't need to be Mickey Mouse,...
1
1738
by: Anders Berg | last post by:
Hi! I'm developing a very simple chat application in VB.NET and I've stumbled into a problem. For simplicity I'm using the TcpListener and TcpClient classes. However, at one point I discovered that I want to use the protected Active and Client properties of the TcpClient class. To do this I made my own derived class: Public Class MyTcpClient Inherits TcpClient
9
2607
by: KraftDiner | last post by:
I have a class class MyClass(MyBaseClass) def __init__(self) super(self.__class__, self).__init__() self.type = MyClassType return self It has a few methods... I have another class and the only difference is the __init__ method..
11
1526
by: Ryan Krauss | last post by:
I have a set of Python classes that represent elements in a structural model for vibration modeling (sort of like FEA). Some of the parameters of the model are initially unknown and I do some system identification to determine the parameters. After I determine these unknown parameters, I would like to substitute them back into the model and save the model as a new python class. To do this, I think each element needs to be able to read...
4
2217
by: Zerofury | last post by:
Okay here's what i'm trying to do. I want to create an array and fill it with objects, so that when i create a method to alphabetize them, all of the pieces (the price, name, ect) don't get all out of order and correlate to each other. Here is my code so far... any ideas? Just need to create an array of classes. How do i get all these items i created into it? Please just give me a quick explanation of the syntax, maybe an example... don't...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.