This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • 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: 9509
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