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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:10:49+00:00 2026-06-07T16:10:49+00:00

So I am trying to figure out why a program is compiling the way

  • 0

So I am trying to figure out why a program is compiling the way it is, hopefully you guys can explain it for me.

class Vehicle{
   public void drive() throws Exception{
     System.out.println("Vehicle running");
   }
}

class Car extends Vehicle{
   public void drive(){
      System.out.println("Car Running");
   }

   public static void main(String[] args){
      Vehicle v = new Car();
      Car c = new Car();
      Vehicle c2 = (Vehicle) v;

      c.drive();
      try {
          v.drive();
      } catch (Exception e) {
          e.printStackTrace();
      } //try v.drive()

      try {
          c2.drive();
      } catch (Exception e) {
          e.printStackTrace();
      } //try c2.drive()
   }
}

So the output for the above program is going to be

Car Running
Car Running
Car Running

My question is, why do I have to do a try/catch block to call drive() method for the v and c2 objects but not the c? They are all instance of Car so what’s happening here?

  • 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-07T16:10:50+00:00Added an answer on June 7, 2026 at 4:10 pm

    Vehicle has a drive() method that throws an exception.

    Car overrides the Vehicle‘s Drive() method with it’s own Drive() method which does not throw an exception.

    The reason you get the output that you do is because even though Vehicle v is of type car, the compiler doesn’t know that fact at compile time, so when you call v.drive() the compiler doesn’t know that you’re calling Car’s drive method.

    Let’s say that you instantiated v in the following way:

    Vehicle v;
    if(rand(0,1) == 1)
        v = new Car();
    else
        v = new Vehicle();
    

    You wouldn’t know whether or not v is a car when you compile. You wouldn’t know until you run the program.

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

Sidebar

Related Questions

I'm trying to figure out a way to make demos for a program I've
I've been trying to figure out a way that I can cpp progressively slow
I am trying to figure out how to create a java program that can
I'm writing a program using PyQt and I'm trying to figure out a way
I'm trying to figure out a way to create a new class instance when
I'm trying to figure out a way to program a function that will de-obfuscate
I am trying to figure out how to get the program to create a
Trying to figure out how I can do this properly. The print_r looks like
Trying to figure out the best way to set up collection lists for users
I'm trying to figure out correct way how to bind something like this with

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.