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 8476151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:06:02+00:00 2026-06-10T18:06:02+00:00

please have a look at this code here. class Vehicle { public void printSound()

  • 0

please have a look at this code here.

class Vehicle {
    public void printSound() {
        System.out.print("vehicle");
    }
}

class Car extends Vehicle {
    public void printSound() {
        System.out.print("car");
    }
}

class Bike extends Vehicle{ // also tried to extend Car
    public void printSound() {
        System.out.print("bike");
    }
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        Bike b = (Bike)v;
        v.printSound();
        b.printSound();

        Object myObj = new String[]{"one", "two", "three"};
        for (String s : (String[])myObj) System.out.print(s + ".");


    }
}

Executing this code will give ClassCastException saying inheritance.Car cannot be cast to inheritance.Bike.

Now look at the line Object myObj = new String[]{"one", "two", "three"};. This line is same as Vehicle v = new Car(); right? In both lines we are assigning sub class object to super class reference variable. But downcasting String[]myObj is allowed but (Bike)v is not. As mentioned in the comment I also tried to extend Car using bike. According to some discussion here, Bike is not a car because it is extending vehicle. If I extend Car by a Bike, then it means Bike is a type of Car, still the exception remains.

Please help me understand what is going on around here.

P.s – please don’t take the whole convert car to bike, bike to car literally 😉

  • 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-10T18:06:04+00:00Added an answer on June 10, 2026 at 6:06 pm

    No, the code provided differs from the code in your example in a basic sentence:

    //you're declaring a Object class variable
    Object myObj = new String[]{"one", "two", "three"};
    //you're declaring a Car class instance, not a Vehicle
    Vehicle v = new Car();
    

    They’re not the same. In the first example, you’re using the parent class to save the value, in the second you’re using a child class and assigning a parent value, but the object will be a child, not the parent.

    Let’s see the classes composition for further explanation:

    Object
    - String[]
    - Vehicle
      - Car
      - Bike
    

    As you can see, every String[] will be a Object, now every Car will be a Vehicle, but a Car is not a Bike. Explaining it with code

    Vehicle v = new Car();
    //v contains an instance of Car
    Car c = v;
    //a Car is not a Bike, this line will throw an error
    Bike b = c;
    //v2 contains an instance of Vehicle
    Vehicle v2 = new Vehicle();
    //a Car is a Vehicle
    Car c2 = v2;
    //a Bike is a Vehicle
    Bike b2 = v2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Hi please have a look on bellow code. <?php class A { public $name;
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Suppose I have following code: public class CBase: AbstractC,IRenderable { //code here } public
Please have a look at the following code. In here, the OK button is
Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
We have some code that looks like this: class Serializer { public: template<class Type>
Sorry if I'm missing something obvious here...but please take a look at this code

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.