There is class T:
public class T
{
protected String name;
public T(String name)
{
this.name = name;
}
public String toString()
{
return "T:"+this.name;
}
}
Class G:
public class G extends T
{
public G(String name)
{
super(name);
}
public String toString()
{
return "G:" + super.toString();
}
}
When I run
G a3 = new G("me");
System.out.println((T)a3)
It prints G:T:me
.
I don't understand why. I thought it would print T:me
. I claim this because it was casted as object T. And therefore, using the toString()
of class T. However, I'm wrong. Why does this happen?
I know there are not good names for classes, it's a question about understanding polymorphism and inheritance, not just to write a specific code I need.