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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:19:44+00:00 2026-05-17T22:19:44+00:00

I am trying to understand Java’s polymorphism, and I have one question about downcasting

  • 0

I am trying to understand Java’s polymorphism, and I have one question about downcasting an object.
Let’s say for this example I have two subclasses Dog and Cat that inherit from a superclass Animal

From what I understood, the only way to downcast an object is if this Object is already of the good type, like this:

Animal a = new Dog();
Dog d = (Dog) a;

This works right?

But what if I want to create a regular animal without knowing what it would be, and then cast it when I know, how can I do that?

Animal a = new Animal();
Dog d = (Dog) a;

This will throw a ClassCastException at runtime right?

The only way I found to do that is to create a new Dog constructor that creates a dog from a regular animal:

Animal a = new Animal();
Dog d = new Dog(a);

with

public Class Dog extends Animal{
   public Dog(Animal a){
      super(a);
   }
}

So my question is, how am I supposed to do this?

  • Am I doing it the best way?
  • Am I not supposed to do this at all, if I have to it means my program is not well conceived?
  • Is there a better way I missed?

Thanks a lot!
nbarraille

  • 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-17T22:19:45+00:00Added an answer on May 17, 2026 at 10:19 pm

    If you want to create an instance of a type that may vary depending upon non-local conditions, use an Abstract Factory (as described in the Design Patterns book).

    In it’s simplest form:

    interface AnimalFactory {
        Animal createAnimal();
    }
    
    class DogFactory implements AnimalFactory {
        public Dog createAnimal() {
            return new Dog();
        }
    }
    

    Note also there is a difference between the static type of a reference and the dynamic type of the object. Even though you have an Animal reference, if the original object is a Dog, it still behaves like a Dog.

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

Sidebar

Related Questions

i'm trying to understand java behaviour. Using this interfaces : public interface IA {}
I have been trying to understand the use of primitives in Java and C#
I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet .
One of the lines in a java file I'm trying to understand is as
In Java, is null an existing object on the heap? I'm trying to understand
I was trying to understand something with pointers, so I wrote this code: #include
I'm trying to understand Java reflecton and am encountering difficulties when working with non-Integer
Just trying to understand how Java annotations work under the covers. Seeing as spring
Trying to understand resources in java-land. I believe the following is true: Resources loaded
I'm trying to understand what the Java java.security.Signature class does. If I compute an

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.