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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:23:11+00:00 2026-06-07T13:23:11+00:00

I have a variable: Abstract a . It is a class that extends the

  • 0

I have a variable: Abstract a. It is a class that extends the Abstract class. However, I need to cast this Abstract variable into a more specific class variable that extends the Abstract class.

My situation: I have two classes, Class1 and Class2 that both extend the Abstract class with methods implemented in each one. I now have an Abstract class variable to work with. I do not know if it is Class1 or Class2, so I cannot simply do a (Class1) a or a (Class2) a (casting).

So how would I successfully cast this variable so that I can use the inner methods?

I was thinking along the lines of using a.getClass().getName() to determine how to cast it, but I am stuck from here on out.

  • 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-07T13:23:13+00:00Added an answer on June 7, 2026 at 1:23 pm

    Your new question appears to be asking how to dynamically cast a variable to an arbitrary type unknown at runtime. This is probably a duplicate of java: how can i do dynamic casting of a variable from one type to another? but to summarize, this is not (easily) possible, isn’t recommended, and speaks to other issues in your code.

    Think about it this way, what variable would you possibly be able to use to store your newly cast object? Imagine if we had a (child) cast operation in Java, that took a variable defined as a parent class, and cast it down to its child (e.g. List -> LinkedList):

    public static void func(Abstract a){
        ???? var = (child)a;
        // Do something with var?
    }
    

    Notice that 1) there’s no way you could ever specify a type for var, since we don’t know at runtime what type it will be; and 2) there’s nothing we’d be able to do with var beyond the behavior defined in Abstract anyways, because the compiler can’t predict which methods will be availible to var other than what’s available to Abstract.

    If you need to implement class-specific behavior, you should do so inside the class. Have an abstract method which each class has to implement, and which can do whatever you need them to do. Or, if you cannot ensure that, don’t define a function that takes an Abstract as an argument; instead define however many functions that take Class1, Class2, etc. objects as parameters, like so:

    Abstract method to require all child classes behave similarly

    public abstract class Abstract{
      /** Do the class-specific behavior you want to do currently in func */
      public abstract void operation();
    
      public static void func(Abstract a){
        a.operation();
      }
    }
    

    Functions only for classes that can actually handle what you want

    public static void func(Class1 a){
      // do something
    }
    
    public static void func(Class2 a){
      // do something
    }
    

    Again, if neither of these options are viable for you (and of course, blocks of instanceof calls aren’t acceptable) then I’d be willing to bet money there’s something structural in the way you’re using Java that’s fundamentally incorrect. If you want to post a code sample of exactly what you’re trying to accomplish by child-casting, perhaps we can shed some light as to what the issue is.


    Leaving this here for posterity – OP’s original question asked about creating new instances of an object cast as its abstract parent.

    Pretty straightforward, get the object’s class object, and create a new instance. For more complex constructors, see the Java documentation on creating new instances dynamically.

    public class ClassVar
    {
        public static abstract class Abstract
        {
    
        }
    
        public static class Class1 extends Abstract
        {
    
        }
    
        public static class Class2 extends Abstract
        {
    
        }
    
        /**
         * Given an instance of a child of Abstract, returns a new instance
         * of the same class
         */
        public static Abstract newInstance(Abstract obj) throws InstantiationException, IllegalAccessException
        {
            return obj.getClass().newInstance();
        }
    
        public static void main(String[] args) throws InstantiationException, IllegalAccessException
        {
            System.out.println(newInstance(new Class1()).getClass());
            System.out.println(newInstance(new Class2()).getClass());
        }
    }
    

    Result:

    class ClassVar$Class1
    class ClassVar$Class2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an abstract class and I have a variable that should be
I have a base abstract logger class, that has an instance variable that I
I have this abstract class that is used as data repository. public abstract class
I have a variable coming into a stored procedure. This variable can either have
I have a class that is inherited from an abstract base class. class CStateBase
I have an abstract class 'A' and class 'B' and 'C' extends A. I
I am a noob and I need some help. So I have this abstract
I have an abstract class with a protected variable abstract class Beverage { protected
The story goes like this: I have an abstract class called Algorithms and a
I have an abstract java class that implements a couple of its methods, but

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.