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

  • Home
  • SEARCH
  • 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 267345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:32:10+00:00 2026-05-11T23:32:10+00:00

I have a class C. Class E extends it. E e = new E();

  • 0

I have a class C. Class E extends it.

E e = new E();
C c = new C();

Why is

e = (E) c;

Upon further review: though numeric conversions have the same syntax as casting objects, some confusion arose. At any event, the above does not give a compilation, but rather a runtime error – so a class can be casted to subclass in some instances (otherwise the code would not compile). Any examples that anyone can give where the above works?

And also:

K extends M

K k = new K();

((M) k).getClass() gives K. Why is that? It was casted to the more general M!

Suppose I have a doIt() method implemented in both M and K. executing

((M) k).doIt();

gives M’s or K’s doIt()?

Thanks!

  • 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-05-11T23:32:10+00:00Added an answer on May 11, 2026 at 11:32 pm

    Consider a real-world example:

    public class Dog extends Animal
    

    All dogs are animals, but not all animals are dogs. Hence…

    public class Cat extends Animal
    

    Casting an Animal to a Dog can only be done if the Animal in question is indeed a Dog. Otherwise it would force the Universe to infer properties unique to a dog (wagging tail, barking, etc.) onto an Animal. That Animal might well be a Cat with properties unique to it (purring, rigorous regime of self-cleaning, etc.). If the cast is not possible then a ClassCastException is thrown at runtime.

    Nobody wants a dog that purrs.


    ((M) k).getClass() gives K. Why is that? It was casted to the more general M!

    You’ve casted k to M, but all classes have a getClass() method. k’s class is always K, regardless of whather you cast its reference to M or not. If you cast a Dog to an Animal and ask it what animal it is it’ll still answer that it’s a dog.

    In fact, casting to a superclass is redundant. A Dog already is an Animal and it has all the methods of an Animal as well as its own. Many Code Analysis tools such as FindBugs will notify you of redundant casts so you can remove them.


    Suppose I have a doIt() method implemented in both M and K. executing

    ((M) k).doIt();

    gives M’s or K’s doIt()?

    K’s doIt() for the same reasons as above. The cast operates on the reference; it doesn’t transform an object to a different type.


    Can you give an example of when casting (Dog doggy = (Dog) myAnimal) makes sense?

    Sure can. Imagine a method that receives a list of animals for processing. All the dogs need to be taken for a walk, and all the cats need to be played with using a bird-shaped toy. To do this we call the takeForWalk() method that only exists on Dog, or the play() method which only exists on Cat.

    public void amuseAnimals( List<Animal> animals ) {
        for ( Animal animal : animals ) {
             if ( animal instanceof Dog ) {
                 Dog doggy = (Dog)animal;
                 doggy.takeForWalk( new WalkingRoute() );
             } else if ( animal instanceof Cat ) {
                 Cat puss = (Cat)animal;
                 puss.play( new BirdShapedToy() );
             }
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 142k
  • Answers 142k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes - the C standard ISO/IEC 9899:1999 a.k.a. C99 (and… May 12, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer Hard to tell. I can't see any obvious mistakes. All… May 12, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer I have VSS and VisualSVN on the same computer so… May 12, 2026 at 8:20 am

Related Questions

I think, the following can't be done in Java. But I would be happy
I have a Set class (This is J2ME, so I have limited access to
I have an interesting situation and I'm wondering if there is a better way
I wrote this simple test code, by adapting a piece from a book, to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.