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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:18:22+00:00 2026-06-17T16:18:22+00:00

Imagine a class Cottage extending Building and the code Building building = new Building();

  • 0

Imagine a class Cottage extending Building and the code

Building building = new Building();
Cottage cottage = (Cottage)building;

Now, it totally makes sense that Building cannot be casted to Cottage considering the nature of java’s inheritance, but what doesn’t make sense (to me) is that this compiles. Why does it compile and then throw a runtime ClassCastException?

Isn’t it obvious that building is a reference to a Building object before actually running the program?

Being such a general question, I know I am getting a possible duplicate for this one 🙂 but I couldn’t find an answer to WHY DOES IT COMPILE question 🙂

EDIT2 I accepted a great answer here (let alone the discussion below it 🙂 ), but I still find the accepted answer in Java casting resulting in run-time error instead of compilation error the most interesting…

EDIT I edited IllegalCastException and put the correct ClassCastException

  • 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-17T16:18:23+00:00Added an answer on June 17, 2026 at 4:18 pm

    That’s because, compiler does not know what object does your reference Building is referring to.

    So, in the below case, where you have a base class reference, pointing to sub class object: –

    Building building = new Cottage();
    Cottage cottage = (Cottage)building;
    

    It would work perfectly fine. So, it’s completely a runtime decision, as to whether it is a valid cast or not. Hence, compiler won’t throw error for that.

    Isn’t it obvious that building is a reference to a Building object
    before actually running the program?

    No. Absolutely not. The type of object being referenced, is not known till runtime. Always remember that, Compiler always checks the reference type. Actual object type is checked at runtime.

    This concept is known as Polymorphism, where you can have the same reference type to point to objects of various subtypes. You can google it, and would get lots of resources to read.

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

Sidebar

Related Questions

Imagine you have class A which has code which runs as method M. And
Imagine the following class that manages a resource (my question is only about the
Imagine this code: class foo { foo fff; int m_id; f & work() {
Imagine that I have a nice Deck class, in the best OO fashion. It
Code below is pseudo code. Imagine a class Fruit which has a factory method
Imagine a class that is built on revolve around the state of a single
This one has been puzzling my for some time now. Let's imagine a class
Imagine a Java class which has most features that you can find in a
I want to close the window after loadFinished complete. imagine simple code: class Example(QWebView):
Imagine a class within a git project that has undergone 1000's of commits, re-visited

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.