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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:08:16+00:00 2026-06-17T16:08:16+00:00

I have 2 classes Class A & Class B where Class A extends Class

  • 0

I have 2 classes Class A & Class B where Class A extends Class B

Now in the constructor of Class A , I have

Class A(Integer integerParam){

     B superclass = new B(integerParam);

}

and the constructor of Class B is as

Class B(Integer integerParam) {

    System.out.println(integerParam);
}

In addition to this I have a few methods in Class B as follows

public void ClassBMethod(){
     System.out.println(integerParam);
}

I want to use reflection to invoke the Super Class method ClassBMethod, and I create an Instance of the Super class to do so like this

Class superClazz = Class.forName(classInstance.getClass().getSuperclass().getName());
Constructor superClassconstructor = superClazz.getConstructor(new Class[]{Integer.class});
Object superclassInstance = superClassconstructor.newInstance(integerParam);

The problem is here the instance of the Super class is getting created twice, once through the Class A constructor and second on reflection.

How can I call the avoid this and call the Super class method without creating the instance

  • 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-17T16:08:16+00:00Added an answer on June 17, 2026 at 4:08 pm

    I’m not a 100% sure, but I think you might be wanting to do this…

    A classInstance= new A(42);
    Class superClazz = Class.forName(classInstance.getClass().getSuperclass().getName()); // or just superClazz = classInstance.getClass().getSuperClass();
    Method classBMethod = superClazz.getMethod("classBMethod");
    classBMethod.invoke(classInstance); // Invoke classBMethod on existing instance, without using any constructor reflectively
    

    In other words, if I understand correctly, you dont want the constructor to be called twice? You want to call the classBMethod() on the already existing instance?



    Besides, the constructor for A looks pretty weird. Judging from the signature of the constructors, it feels at it would make most sense for the A constructor to be:

    public A(Integer integerParam){
        super(integerParam);
    }
    

    An explanation of this:

    Lets compare the classes A and B to more concrete objects:

    class A <--> class Apple
    class B <--> class Fruit
    Integer integerParameter <--> double weight
    

    If you are doing the constructor:

    public Apple(double weight){
        super(weight);
    }
    

    You are essentialy saying that when you create an apple (new Apple(someWeight);) that apple IS a fruit with weight == someWeight.

    If you are doing:

    public Apple(double weight){
        Fruit b = new Fruit(weight);
    }
    

    That means: when you are calling new Apple(someWeight) you create a different fruit b(not the apple itself) which you give someWeight. So essentially what you have is an Apple with unspecified weight, and another unknown fruit which weighs someWeight. Since the other fruit is declared within the constructor, as soon as the constructor is finished, the other fruit will disappear. The only thing that will be left is an apple with unspecified weight (or with a default weight specified by the default constructor).

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

Sidebar

Related Questions

I have 2 classes, say A & B: Class A extends B { public
If I have three classes class A class B extends A class C extends
I have the following class structure: public abstract class Generic<T extends SuperClass> public class
I have the following classes: public class Obj<T> extends BaseModel { public static final
I have two test classes called TT_Common and TT_Container which extends CPPUNIT_NS::TestFixture: class TT_Common
Say I have classes class A{ //code for class A } class B{ //code
I have two classes class A { public string something { get; set; }
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
Suppose we have two classes: class Base { private: int x; public: void f();
Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true

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.