• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: http://www.go4java.20m.com/mock1.htm

class Message
{
String text="Hello1";
}
class Super
{
Message msg=new Message();
}
class inheritance extends Super
{
public static void main(String arg[])
{
inheritance i=new inheritance();
i.print();
}
public void print()
{
System.out.println(super.msg.text);// Prints Hello1.
}
}

Here msg.txt as well as super.msg.text prints the same ouput, how is that possible?
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is possible because super.msg and msg point to the same object. Since there is not object in Inheritance class with the name msg, so both the syntax will point to the same object...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic