wood burning stoves 2.0*
The moose likes Java in General and the fly likes Parent / Child -- Pls. Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parent / Child -- Pls. Help" Watch "Parent / Child -- Pls. Help" New topic
Author

Parent / Child -- Pls. Help

Raj Kumar
Ranch Hand

Joined: Sep 19, 2005
Posts: 65
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

Joined: Jan 14, 2006
Posts: 139
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

Joined: Sep 19, 2005
Posts: 65
Thanks ya got the ans...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parent / Child -- Pls. Help
 
Similar Threads
does inheritance break the encapsulation or data hiding?
Accessing component from main class.... Help!
Method Overriding
calling a method
Protected members