1

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

Vivek
  • 2,091
  • 11
  • 46
  • 61
  • 1
    That is the essence of Java. You can't avoid. Besides, read up on Java naming conventions too. – adarshr Jan 23 '13 at 09:56
  • Related- not duplicate though - http://stackoverflow.com/questions/1878558/jump-over-parent-constructor-to-call-grandparents – Jayan Jan 23 '13 at 09:58
  • So what would be the workaround for this, if I had to avoid it. – Vivek Jan 23 '13 at 10:02

3 Answers3

3

Whenever the constructor of a class is called, the constructor of its super-class is called and then the constructor of the class above in the hierarchy up to Object. There is no way to avoid that.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
2

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).

Alderath
  • 3,761
  • 1
  • 25
  • 43
1

You can just write in the class A: super.ClassBMethod().

If you need to use reflection (is it a home work?), then follow the answer of Alderath. I only would simplify one line there:

Class superClazz =classInstance.getClass().getSuperclass()

or just

Class superClazz = B.class;
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38