Reputation: 1413
When super() is called from subclass, it is invoked on 'this' object i.e the object created by the statement new Subclass() . super() is called on 'this' object. But 'this' object contains all variables,methods of superclass and itself as well as its constructors but not superclass constructor. So how is super() invoked on 'this' object?
Please see the image attached in which I have tried to explain my understanding.
Note: P is super class and Q is derived class
Upvotes: 1
Views: 88
Reputation: 43401
But 'this' object contains ... not superclass constructor.
It does "contain" the superclass constructor (insofar as it "contains" any method, which is a bit hand-wavy) — it's just that the only way to invoke that super constructor is to use the super(..)
syntax, along with its constraints (specifically, that it has to be the first statement of the subclass's constructor).
On that hand-waving: an object doesn't actually contain a method. Methods are static things that are invoked on arguments, and when you invoke this.foo()
it essentially gets translated (by the JVM) to foo(this)
. The name this
is really just a special name for "that first argument to the method, which happens to always be what I expect this
to be."
Constructors work exactly the same way. new Foo()
allocates some memory, initializes its variables to their default values (0/null)s, and then invokes Foo.<init>(<a reference to the new chunk of memory>)
. If The Foo constructor invokes super()
, then the first statement in Foo.<init>
is essentially FooSuperClass.<init>(this)
.
That's not how the Java syntax works, but it's basically what's going on.
In fact, check out this bit of Java code:
// in Super.java
public class Super {}
// in Sub.java
public class Sub extends Super {}
if you do javap -c Sub
, you'll see:
Compiled from "Sub.java"
public class Sub extends Super {
public Sub();
Code:
0: aload_0
1: invokespecial #1 // Method Super."<init>":()V
4: return
}
What does this do? At 0
it pushes the first argument to the Sub()
constructor (aka this
) to the stack, then at 1
it invokes Super.<init>
, which will pop that argument and pass it to the Super.<init>
method. (And finally, at 4
it returns.)
Upvotes: 6
Reputation: 264
'this' object does contain the superclass constructor.
Because Q
is a child class (or derived class) of P
, Q
has access to the same methods, fields, and constructors that P does. The super()
constructor will construct an instance of Q
as far as an instance of P
is concerned. Any other initialization will have to take place after your use of super()
. Note that super()
must be the first line in the constructor if you're going to use it.
public Q()
{
super();
//other TO-DO...
}
Upvotes: 3
Reputation: 4900
The default constructor (that is the one without parameters) is always defined.
You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.
See: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
This constructor will always be implicitly called but you can also call it explicitly.
If you add a constructor P(String foo, int bar)
to P you'll have to call that constructor in Q:
public class Q {
public Q() {
super("answer", 42);
}
}
Upvotes: 2
Reputation: 149
In java whenever subclass is instantiated, its super class constructor is called implicitly. That is because, parent class needs to get initialize first to make sure the derived class have all those values.
Upvotes: 2