Should inherited constructor be called in this situation?

S

Steve Sobol

Using reflection...

Class c=Class.forName(classname,false,new URLClassLoader(urlList));
// Get a declared zero-arg constructor
try {
newPlugin = c.getDeclaredConstructor(null).newInstance(null);
} catch(NoSuchMethodException e) {
throw new PluginException(c.getCanonicalName() +
" has no declared zero-argument constructor");
} catch(InvocationTargetException e) {
throw new PluginException("Could not call constructor for " +
c.getCanonicalName());
}


newPlugin is an instance of Class B, which extends class A.

When I call newInstance, class A's constructor is called first, then class
B's constructor, even though Class B doesn't have a call to super().

If I remove Class B's constructor, class A's instructor is still called. I'd
have expected getDeclaredConstructor to throw an exception or return null if
there was no actual constructor declared in B...

Is this the correct behavior?
 
P

Patricia Shanahan

Steve said:
Using reflection...

Class c=Class.forName(classname,false,new URLClassLoader(urlList));
// Get a declared zero-arg constructor
try {
newPlugin = c.getDeclaredConstructor(null).newInstance(null);
} catch(NoSuchMethodException e) {
throw new PluginException(c.getCanonicalName() +
" has no declared zero-argument constructor");
} catch(InvocationTargetException e) {
throw new PluginException("Could not call constructor for " +
c.getCanonicalName());
}


newPlugin is an instance of Class B, which extends class A.

When I call newInstance, class A's constructor is called first, then
class B's constructor, even though Class B doesn't have a call to super().

If I remove Class B's constructor, class A's instructor is still called.
I'd have expected getDeclaredConstructor to throw an exception or return
null if there was no actual constructor declared in B...

Is this the correct behavior?

Yes. There are two default rules in operation here:

1. The first statement in the body of every constructor, except in class
java.lang.Object, is a constructor invocation using "super" or "this".
If the constructor body does not begin with an explicit constructor
invocation, and the class is not java.lang.Object, the compiler inserts
one equivalent to "super();".

B's constructor is really called first, but its implicit first statement
is "super();", so the rest of its body does not execute until A's has
executed.

2. Each class contains at least one constructor. If there is no
explicitly declared constructor, the compiler creates a default
constructor that takes no parameters and only contains "super();".

That is the constructor your getDeclaredConstructor call finds when B
has no explicit constructor declarations.

Patricia
 
B

Babu Kalakrishnan

Steve said:
If I remove Class B's constructor, class A's instructor is still called.
I'd have expected getDeclaredConstructor to throw an exception or return
null if there was no actual constructor declared in B...

Is this the correct behavior?

public class Parent()
{
public Parent(int i)
{
// Some code
}
}

public class Child extends Parent
{
}

You'll find that the class Child will fail to compile even if it
contains nothing in its body :)

Illustrates bothe the points which Patricia mentioned. The class Child
implicitly contains a constructor :

public Child()
{
super();
}

whereas the class Parent *does not have* this constructor because the
class already contains an explicitly declared constructor. So the
super() call in the Child class is illegal and causes a compiler error.

(Also there is a hidden super() call inside the "Parent" class's
declared constructor as its first line if you haven't put anything in
explicitly)

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top