Reputation: 837
What is the need for the JVM to call superclass constructor?
public class Test1{
public void dis()
{
System.out.println("In Test1");
}
}
public class Test2 extends Test1{
public void dis()
{
System.out.println("Inside test2");
super.dis();
}
}
Since dis()
is an instance method, is it safe to assume that super is pointing to parent class object since the constructor is automatically called?
Upvotes: 0
Views: 494
Reputation: 1
using super constructor we can call functions and variables directly from
super class in subclass for
public class Test1{
int a=5;
}
public class Test2 extends Test1{
int a=15;
int c;
c=a+super.a;
System.out.println("value of c is:"+
}
result :value of c is:20
you can call default constructor of super class by super() .
if there is variables then you can call it by super(parameter_list).
Upvotes: 0
Reputation: 213401
is it safe to assume that super is pointing to parent class object
super
keyword is used to access direct super class members on the current object. It does not refer to any super class object.
since constructor is automaticallt called?
The role of super
keyword is not dependent upon the super class constructor being called. Super class constructor is always called through constructor chaining using super()
call to either no-arg or parameterized constructor.
One difference between chaining super class constructor, and invoking super class member is that: "While chaining constructors, the super()
call has to be the first statement in constructor, while you can invoke super class method at any point in your current method".
Upvotes: 1
Reputation: 234857
The keyword super
does not point to some "parent class object". It is a name qualifier so you can refer to a method defined in the parent class for the current object. Thus, in the following:
public class Test2 extends Test1{
public void dis()
{
System.out.println("Inside test2");
super.dis();
}
}
the call super.dis()
invokes the dis()
method defined in the parent class for this object. It does not invoke the dis()
method of some other object named super
.
Slightly different things are going on with constructors. Every constructor must always start with a call to some constructor in the parent class. You can do this explicitly using the super
keyword:
public class Test2 extends Test1{
public Test2() {
super(); // explicitly invokes parent class default constructor
. . .
}
}
If you don't explicitly call a particular parent class constructor, the compiler automatically inserts a call to the default (no-argument) parent class constructor. The call to a parent class constructor (if present) must be the first statement in a constructor. Within a method, however, super.
can be used as a qualifier for any name wherever it is used (provided the name is a member of the parent class).
Upvotes: 1
Reputation: 73578
This has nothing to do with constructors. Since Test2 extends Test1
and dis() is implemented in both classes, super.dis() in the subclass will call dis() in the superclass.
Upvotes: 0
Reputation: 236150
The super
keyword is useful for:
Your example shows the first case in action, when calling dis()
on an instance of Test2
the program must print, in this order:
Inside test2
In Test1
Upvotes: 1