Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:23:53+00:00 2026-06-14T03:23:53+00:00

Based on a older question of mine Link I’m working on learning more about

  • 0

Based on a older question of mine Link I’m working on learning more about Casting and Instanceof. That is based upon a scenario described in a HeadFirst book

So basically I’ve now got a new class(Hybrid) that inherits from my Vehicle class what i’m trying to do is cast a Hybrid Object to display the extra information that comes with being a hybrid. It complies but doesn’t really give me any idea what is causing the error except it just ends on the line i’ve marked.

public class ShowroomDriver {
    public static void main(String[] args) {
    Showroom cars = new Showroom("Cars");
    Hybrid hybrid1 = new Hybrid("Toyota Prius", "Focus", "John Smith", "TOTAP453453987346283",
            getCalendar(2,3,1998), getCalendar(24,2,2012),
            "Right Hand",//Hybrid Only Info Edit: Forgot to commentout 
            true,
            'C',
            650, 82.0); //Cost & (Hybrid MPG)

    cars.addVechicle(hybrid1);
    cars.getVechicles();

Hybrid Class

import java.util.Calendar;

public class Hybrid extends Vehicle{
    private double consumption;
    private String drive;

    public Hybrid(String Manufacture, String Model, String CustomerName, String Vin, 
            Calendar DateManufactured, Calendar Datesold, String Drive,
            boolean HasbeenSold,
            char TaxBand,
            double Cost, double Consumption){

        super(Manufacture, Model, CustomerName, Vin, DateManufactured, Datesold,
                HasbeenSold,
                TaxBand,
                Cost);
        this.consumption = Consumption;
        this.drive = Drive;
    }

    public Double getConsumption() { return this.consumption; }
    public String getDrive() { return this.drive; }
}

New Vehicle Method

public void displayDetails(){
    for(int i = 0; i <cars.theVehicles.size(); i++){
        if(this.cars.theVehicles.get(i) instanceof Hybrid){//Error here
            Hybrid thehybrids = (Hybrid)this.cars.theVehicles.get(i);
            System.out.println("Consumption: " + thehybrids.getConsumption()+ "\n" +
                    "Drive: " + thehybrids.getDrive());
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:23:54+00:00Added an answer on June 14, 2026 at 3:23 am

    Do you need to cast ? You’ve already overridden the displayDetails() method to display hybrid-specific info. So you should just be able to call this and the runtime will determine the correct method to call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen and worked with a lot of older, JDBC-based DAO code that usually
This question is based on this older one (not by me). I need to
based on .designer.cs, i infer that the menu's arrangement is based on the order
I have a tableView that's loosely based on the DetailViewController in ye olde SQLiteBooks.
I'm trying to write a named scope that will order my 'Products' class based
In an answer to an earlier question of mine regarding fixing the colorspace for
My question is similar to sql statement to delete records older than XXX as
I've not found anything that addresses my specific name space question as yet. I
Yet another question about which NoSQL to choose. However, I haven't found yet someone
So I'm starting to hear more and more about Web Workers . I think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.