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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:47:54+00:00 2026-06-16T01:47:54+00:00

I have an abstract class A I have about 10 classes that extend A

  • 0

I have an abstract class A

I have about 10 classes that extend A

Class A has one or two static methods and it makes sense that these are static, because they belong to the 10 classes, NOT instances of them. One static method e.g. is called getAllFromX, which gets all all instances of the class from X, whatever that may be, it may be a server, well it actually is, but it doesn’t matter. So you see it makes sense these methods are static and are not bound to an instance.

At the same time class A has a NON-static abstract method, each subclass overrides this method (just returns a string). I cannot make it static because static methods cannot be overridden (…).

To summarize: abstract class A has a static method and a abstract non-static method, that is overriden by the subclasses. I cannot make the second method static because it must be overriden. On the otherhand I could make the first method non-static, but it would be very ugly and bad programming style, so I’ll leave it that way.

The catch? The static method in class A must get the value the non-static method returns (for the subclass the static method is inherited from, of course).

Is the “easiest” way to use reflection to get this done? I mean…really?

Like e.g., I get the class the static method is in:

Class<?> cl=new Object(){}.getClass().getEnclosingClass(); (a hack I found here, thank god...)

I then use getConstructor to construct an object of this subclass.

And then I use this object to call the non-static method.

Really?? Can it not be done easier? I mean that is if I want to design my program conceptually correct…

Coming from C# I don’t like that (and the type erasure thing). It is just ugly. Doable but ugly. And a big stumbling block, at least for beginners. EDIT: after reading it again, I’d add: /rant end. Sorry, but I actually care.

  • 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-16T01:47:56+00:00Added an answer on June 16, 2026 at 1:47 am

    You can always use reflection to invoke a method using class name e.g.

        Object objectX = ClassX.class.newInstance();
        //get your method passing argument types as second param
        Method method = ClassX.class.getDeclaredMethod("methodX", null);
        //invoke your method passing arguments as second param
        method.invoke(objectX, null);
    

    Since you mentioned your static method doesn’t use any instance but you are using reflection to get the instance hence I am really not sure, how does it fit in your requirement though.

    I think making it as an implemented method (non-static) in your abstract class is a better choice. That way you implement it once but its available in in all your 10 extending classes.

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

Sidebar

Related Questions

I have a base class with a static factory method that makes derived classes:
I have an abstract class that has a virtual method. The method is virtual
I have one abstract class -let's say myBase. And I want all the classes
I have one abstract class and many child classes. In child classes are from
I have 4 classes, one Database helper and 3 defined like this: abstract class
We have a set of about two dozen classes that inherit from a base
I have an abstract class Exception in custom namespace which has no abstract methods.
I have a base abstract class with several classes which extend and override it,
I have an abstract class that is inherited by a number of other classes.
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor

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.