The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

confused ????

Posted by saddy on January 22, 2001 at 12:15 PM

Here is a short program

class A
{
int i = 10;
void meth()
{
System.out.println(" i am in A");
}
}


class B extends A
{
int i =20;
void meth()
{
System.out.println(" i am in B ");
}

public static void main(String args[])
{
A a = new B();
System.out.println(" i = "+a.i);
a.meth();
}
}

on executing the above program the output is:
i = 10
i am in B
that is object a is pointing instance variable of A where as invoking methods from class B although instance variable with the same name i.e. i(here) is present in the base class .
can anybody please help me why it is behaving like this.
Thanx in advance




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us