Sunny
Sunny

Reputation: 14808

Explain the output of the following Java program

package com.test;

public class Main {

    public static void main(String[] args) {

        System.out.println(new B().toString());
    }
}


package com.test;

class A {

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getClass().getName();
    }
}

package com.test;

public class B extends A {

}

This program gives output com.test.B but if I change toString method of class A to

@Override
public String toString() {
    return "hello";
}

Then it print hello. why?

Upvotes: 0

Views: 340

Answers (3)

Deepak Bhatia
Deepak Bhatia

Reputation: 6276

In your first function call when the method is:

@Override
public String toString() {
        // TODO Auto-generated method stub
        return this.getClass().getName();
}

Since this method is invoked from the B class instance then this.getClass() refers to B class object. Thus getName() function prints

com.test.B

If the same function would have been invoke by creating A class object then output would have been,

com.test.A

And when you change the toString function to this:

@Override
public String toString() {
    return "hello";
}

it will return hello, as you have returned the hello as the return value.

Now if you really want to understand the @Override then add this code to class B and in A class let the function returning hello be there

@Override
public String toString() {
     // TODO Auto-generated method stub
     return this.getClass().getName();
}

Try the above code, and invoke the toString function from class A and class B instance object. Then it will be more clear what @Override does and how it works

Upvotes: 3

SQB
SQB

Reputation: 4078

Well, you're calling toString() on an instance of B and printing that. B has no toString() of its own, but inherits it from A. The toString() defined in A returns the name of the class. So when B is using it, the name of the class is com.test.B, which is what's returned.
If you change the implementation in A to return "hello" instead, that's what it will return.

You may have been expecting the first version to print com.test.A, so I'll explain why that doesn't happen.
It's not that B asks A "what's the result of toString(), B asks A "what do I have to do to get the result of toString(). In the first case, A tells B "just return your (class) name", while in the second case, A tells B "just say 'hello'".

Upvotes: 1

Debojit Saikia
Debojit Saikia

Reputation: 10632

In this toString method:

@Override
public String toString() {
        // TODO Auto-generated method stub
        return this.getClass().getName();
}

you are returning this.getClass().getName(), which returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

And when you change the toString to this:

@Override
public String toString() {
    return "hello";
}

its returning hello, because you have "hello" as the return value.

Upvotes: 1

Related Questions