0
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?

9
  • Why does the output surprise you? Commented Nov 12, 2013 at 7:54
  • I'd wager good money that the reason the output changed is because you changed it. Commented Nov 12, 2013 at 7:55
  • Ahem, you change the method to print "hello" and then you are surprised that it prints that? The secret behind this is called method overwriting. Commented Nov 12, 2013 at 7:55
  • 2
    I think the issue the OP is having is that it was printing that it is a B and now it's printing hello but the method is in A. I would start with the basics of inheritance to understand. Commented Nov 12, 2013 at 7:57
  • 1
    No one expects the spanish inheritance ... i guess Commented Nov 12, 2013 at 7:58

3 Answers 3

3

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

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

Comments

1

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.

Comments

1

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'".

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.