3

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); } } 
5
  • 2
    Please make sure you stick to Java naming conventions, especially in these formative first programs. In Java we use PascalCase for class names, reserving camelCase for variables. Further, please format your code before posting. Commented Jul 28, 2015 at 16:49
  • Since i am new to java i will soon learn Java naming conventions. @BoristheSpider Commented Jul 28, 2015 at 16:52
  • 4
    @Ashokkumar Instead of learning in future, you should try to edit now and learn. Commented Jul 28, 2015 at 16:54
  • @karthik Yeah. I have formatted the code little bit. from now on i will use proper naming conventions. Commented Jul 28, 2015 at 16:58
  • @Ashokkumar don't format code by hand! That's a fruitless task. Ask your IDE to autoformat it for you. Commented Jul 28, 2015 at 16:59

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments