Reputation: 1413
Please allow me to explain my understanding.
class P
{
P()
{
System.out.println("hi "+this);
/*which object of P is currently executing this constructor?
Is "this" here object referenced by q or some other object?*/
}
}
class Q extends P
{
Q()
{
super();
/*constructor of superclass called on this object(referenced by q) */
}
}
class R
{
public static void main(String args[])
{
Q q = new Q(); //constructor Q() invoked on object referenced by q
}
}
So my doubts are: 1. super() is invoked on which object of P(since none exists). 2. What is "this" in P() referring to? Is it the same object as q? In other words is this=q ?
Upvotes: 3
Views: 87
Reputation: 957
Constructors are not inherited in java.
As per the official Java tutorial:
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
When you call super()
you simply invoke the parent constructor, which can access private members of the class it belongs to.
As for the updated version of your question:
As mentioned above, super()
simply invokes the parent constructor. It's not invoked on any object, since constructor is not a member of a class.
this
in the P
constructor will refer to the instance of a subclass (Q
) that is being constructed.
EDIT:
Regarding 2. This can be easily verified by outputting the reference q
in your main
method. It should print exactly the same that the System.out.println(this)
call in the P
constructor.
public class R
{
public static void main(String args[])
{
Q q = new Q(); //constructor Q() invoked on object referenced by q
System.out.println(q);
}
}
Upvotes: 1
Reputation: 12006
Code, ctors included, does not define 'object state'. Set of instance fields do. And, under the hood, subclass instances do include private
fields of their superclasses, but javac
makes sure that they only accessible, directly or indirectly, by calling superclass methods (ctors included).
Regarding your updated question:
class Q extends P
{
Q()
{
super(); /*constructor of superclass called on this object(referenced by q) */
}
}
Here Q extends P, so super()
calls P's ctor. During execution of P's ctor, this
allows to refer to fields and methods belonging to P and to it's supertypes (e.g., Object
) w/ restrictions imposed by access modifiers (I mean you can access only public
and, w/ restrictions protected
and 'package-private' members of subtypes), but not to P's subtypes.
Regarding "no access to P's subtypes" rule there's one more twist: if you call some non-private (overridable) method of P from P's ctor, chances are this method will be overridden someday in Q
or other subtype, and you suddenly start calling some unforeseen code! So, this
inside P
does allow to refer explicitly to P's subtype methods but not to P's subtype fields (although implicitly P's fields can be reached by P's subtype method).
To give an example -- change f()
in following code from private
to 'package-local' and witness a NPE thanks to following execution chain Derived#Derived() -> Base#Base() -> Derived#f() -> NPE!
:
class Base {
private Integer base = 1;
Base() {
f();
}
private void f() {
System.out.println(base);
}
}
class Derived extends Base {
private Integer derived;
Derived() {
super();
derived = 2;
f();
}
private void f() {
System.out.println(derived.intValue());
}
}
Upvotes: 3
Reputation: 11173
Constructors are not inherited to subclasses. And using super()
you can just call the super class constructor. You can not access the private variable/property of super class from subclass. Moreover private member of a super class is also not inherited to it's subclass.
Upvotes: 2