0

Guys can you please explain to me how come the output of this example is:

  Exam1
  This is Tennis Shoes
  derived class
  This is a white Tennis Shoes is created

If you could explain to me what exactly happened after we created an Object of White Tennis Shoes and what happened. Please guys don't tell me do your homework or something I'm just preparing for my exam and I this is an old question.

The code is below:

class Shoes{
    public Shoes(){
        this("you have created a shoes Object");
        System.out.println("This is the base class");
    }

    public Shoes(String s){
        System.out.println(s);
    }
}

class TennisShoes extends Shoes{
    public TennisShoes(){
        this("This is Tennis Shoes");
        System.out.println("derived class");
    }

    public TennisShoes(String s){
        super("Exam1");
        System.out.println(s);
    }
}

class WhiteTennisShoes extends TennisShoes{
    public WhiteTennisShoes(String s){
        System.out.println(s);
    }
}

class ConstructorPrintingTester{
    public static void main(String[] args){
        WhiteTennisShoes shoesObj;
        shoesObj = new WhiteTennisShoes("This is a white Tennis Shoes is created");
    }
}
4
  • 3
    Whether homework or not, why not give your interpretation of what's happening first? Commented Oct 15, 2011 at 13:33
  • I didn't know that when you call this("xxxx") it will call another constructor inside the current constructor that's why it stopped me after thinking 15mins! thanks to phihag it's really clear now and I like the way he listed them down which is very helpful. Commented Oct 15, 2011 at 14:00
  • also I have totally forgot that when you call a super() constructor Java will create one for me. so this is why I really couldn't understand a thing from the output. Commented Oct 15, 2011 at 14:02
  • The easist thing if you have questions like this is to use a debugger. Set a brakpoint and step through your code. Commented Oct 31, 2011 at 14:53

3 Answers 3

3

When you create an object of type WhiteTennisShoes, its constructor is executed:

public WhiteTennisShoes(String s){
    super(); // This line is automatically inserted
    System.out.println(s);
}

Since each constructor must call the super or another constructor in the first line, and doing so makes Java automatically call the paremeter-less super constructor, the TennisShoes constructor with arity 0 is called. The current callstack is then

main(..)
WhiteTennisShoes("This is a white ...")
TenniesShoes()

Now, TenniesShoes() does call another constructor in the first line. The current callstack is now

main(..)
WhiteTennisShoes("This is a white ...")
TenniesShoes()
TenniesShoes("This is Tennis Shoes")

That constructor calls the one-argument super constructor with the argument "Exam1". The callstack is

main(..)
WhiteTennisShoes("This is a white ...")
TenniesShoes()
TenniesShoes("This is Tennis Shoes")
Shoes("Exam1")

Since the one-argument constructor of Shoes does not call a constructor, the implicit one of the superclass Object is now called; the callstack is

main(..)
WhiteTennisShoes("This is a white ...")
TenniesShoes()
TenniesShoes("This is Tennis Shoes")
Shoes("Exam1")
Object()

After that, the callstack is unwound and all the prints are called in order:

  1. Shoes("Exam1") prints "Exam1"
  2. TenniesShoes("This is Tennis Shoes") prints "This is Tennis Shoes"
  3. TenniesShoes() prints "derived class"
  4. WhiteTennisShoes("This is a white ...") prints "This is a white ..."
  5. main prints nothing and exits.
Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much! i'm trying to understand from your steps. but this confuses me a lot since I don't use it. this("This is Tennis Shoes"); what does it mean?
Oh oh I got what it means now after reading your steps. man you were so helpful!!! let me continue. I think it is better for me to do your steps when I came across this kind of question in exam.
2 things I didn't know/awareOF 1-Java creating a Super() for me if I didn't create one 2- Calling a constructor inside a constructor because I never tried it before. But now it's clear. Much Appreciated .
0

When you don't specify a parent constructor to call in a class in Java, the default parent constructor is called during instantiation. In your case, creating a new WhiteTennisShoes will automatically call the default parent constructor, which is TennisShoes(). This will call TennisShoes(), which then calls Shoes(String).

The important thing to remember in this case is that, in Java, instantiating a class calls the constructor of the class and of every parent class. You can specify which parent constructor to call, and if you don't, the default, no-arg, parent constructor is called.

Comments

0

You invoke the WhiteTennisShoes constructor. This constructor has no super() call, so the compiler inserts one for you, and the first thing it does is thus to call the no-arg constructor of its superclass : TennisShoes.

The TennisShoes constructor without any argument invokes the other constructor with "This is Tennis Shoes" as argument. The first this this constructor does is to invoke the constructor of its superclass with "Exam1" as argument.

The Shoes constructor is thus invoked with Exam1 as argument, and outputs Exam1. Then the TennisShoes constructor continues its execution and outputs its argument : "This is Tennis Shoes". Then the TennisShoes no-arg constructor continues its execution and outputs "derived class". Then the WhiteTennisShoes constructor continues its execution and outputs its argument : "This is a white Tennis Shoes is created".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.