I have learned that object should be created something like this Class_name Object_name=new Classname()
. In my program, I created a super class named sup
and a derived class named der
. In my subclass constructor, I have created a object like this sup obc = new der();
. Without any errors it compiled and gave output like this:
In Superclass with object passed as reference
In Superclass with no constructor
In derived class with no constructor
I did not understand how I got the output in this order. Why does this happen? Here is the complete code:
class sup {
private int a, b, c;
sup(sup ob) {
System.out.println("In Superclass with object passed as reference");
a = ob.a + 9;
b = ob.b + 9;
}
sup(int a, int b) {
this.a = a;
this.b = b;
}
sup() {
System.out.println("In Superclass with no constructor");
}
}
class der extends sup {
int d;
der(der ob) {
super(ob);
sup obc = new der();
}
der() {
System.out.println("In derived class with no constructor");
}
der(int a, int b, int c) {
super(a, b);
d = c;
}
}
public class Test {
public static void main(String args[]) {
der ob1 = new der(3, 4, 5);
der ob2 = new der(ob1);
}
}
答案 0 :(得分:9)
The first call:
der ob1 = new der(3, 4, 5);
prints nothing, because first it calls:
der(int a, int b, int c) {
super(a, b);
Which calls:
sup(int a, int b) {
this.a = a;
this.b = b;
}
Which has no System.out.println
statements. None of the other constructors are called, because you never explicitly call them. The implicit sup()
constructor is not called, because you've called super(a, b)
.
The second call:
der ob2 = new der(ob1);
Calls this constructor:
der(der ob) {
super(ob);
sup obc = new der();
}
The first thing that happens is the super constructor here gets called:
sup(sup ob) {
System.out.println("In Superclass with object passed as reference");
This is the first thing that gets printed. Then, the second line:
sup obc = new der();
is called, which calls this constructor:
der() {
System.out.println("In derived class with no constructor");
}
However, there is an implicit call to super();
here that is always called in subclasses, even if you do not write it. So we then call:
sup() {
System.out.println("In Superclass with no constructor");
}
This gets printed, and finally der
's constructor can finish.
Note that if you delete the no-argument constructor of sup
, der
will no longer compile with the error:
Implicit super constructor
ArrayListTest.sup()
is undefined. Must explicitly invoke another constructor
答案 1 :(得分:6)
der(der ob) {
super(ob);
sup obc = new der();
}
The first thing this does is calling the superclass constructor sup(sup obj)
(which prints the first output line), then the code (without any reason) allocates a new der()
.
But in Java, the superclass default constructor is implicitly called before the subclass constructor, indeed der()
constructor implementation could be seen as
der()
{
super();
System.out.println("In derived class with no constructor");
}
which explains second and third output line.
答案 2 :(得分:1)
The first der
object calls the super(a,b)
constructor which has no output. The second calls sup(sup ob)
which gives the first line of output and then it also calls new der()
, when the default constructor for a subclass is called the default constructor for its superclass is also called. That's where the 2nd line comes from. And then new Der()
outputs the 3rd line.