Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parent / Child -- Pls. Help

 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
pls. check the following program

class class1
{
int i = 99;
class1()
{
meth();
}

void meth()
{
System.out.println(" In Class 1 Method 1");
}
}

class class2 extends class1
{
int i = -1;
void meth()
{
System.out.println("In Class2 Method1");
}

public static void main(String args[])
{
class1 obj = new class2();
System.out.println(obj.i);
obj.meth();
}
}




Output:
In Class2 Method1
99
In Class2 Method1


Question :
Why "System.out.println(obj.i);" is printing "99" as a result rather than "-1" pls. give a detailed explanation why java does so...

Thanks in advance
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java supports run-time polymorphism for member function only , and because of this you are getting that output .



Hope i answered you


cheers
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ya got the ans...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic