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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:48:42+00:00 2026-06-17T18:48:42+00:00

I have 2 classes, say A & B: Class A extends B { public

  • 0

I have 2 classes, say A & B:

Class A extends B {
    public void subClassMthd(){
        System.out.println("Hello");
    }
}

Class B {
    public void printHelloWorld {
        System.out.println("Hello");
    }
}

Now, I am using reflection to invoke the methods on Class A. I would also like to invoke the printHelloWorld method present in Class B.

I tried using

Class clazz = Class.forName("com.test.ClassA");
Object classAInstance= clazz.newInstance();
Method superClassmthd = classAInstance.getClass()
    .getSuperclass().getMethod("printHelloWorld", null);
superClassmthd.invoke(classAInstance);

Also tried as

Class clazz = Class.forName("com.test.ClassA");
Object classAInstance= clazz.newInstance();
Class superClazz = Class.forName(classAInstance.getClass().getSuperclass().getName());
Object superclassInstance = superClazz.newInstance();
Method superClassmthd = superclassInstance.getMethod("printHelloWorld", null);
superClassmthd.invoke(superclassInstance );

But none of them work; they throw an InstantiationException.

What am I doing wrong here?

  • 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-17T18:48:44+00:00Added an answer on June 17, 2026 at 6:48 pm

    Try this:

    Method mthd = classAInstance.getClass().getSuperclass().getDeclaredMethod("XYZ");
    mthd.invoke(classAInstance)
    

    The difference is using getDeclaredMethod(), which gets methods of all visibilities (public, protected, package/default and private) instead of getMethod(), which only gets methods with public visibility.

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

Sidebar

Related Questions

Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Say you have two classes, order and customer: public class Customer{ public int CustomerId
I have 2 classes, say A & B. Class B has a destructor of
Lets say I have these classes: class Foo { public $_data; public function addObject($obj)
Say I have two classes, in two different headers, called: class TestA { public:
Say I have an abstract class class NecessaryDanger { public: virtual void doSomethingDangerous() =0;
Say I have classes class A{ //code for class A } class B{ //code
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Say I have two classes, and neither of them are GUI components. Class A
I have the following simple class class base { public: int x; base &set(int

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.