This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance  RSS feed

 
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: 9610
37
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
Boost this thread!