File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another Question.

 
Amisha Shah
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why the output of following program is S1 and not S2 ? please explain.
class Question13
{
String s = "outer";
public static void main(String args[])
{
S2 ob = new S2();
ob.display();
}
}
class S1
{
String s = "S1";
void display()
{
System.out.println(s);
}
}
class S2 extends S1
{
String s="S2";
}
 
Arno Reper
Ranch Hand
Posts: 286
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
because you override the instance variable s and the display method print the s that's defined into the S2 object
arno
 
Amisha Shah
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi arno,

can i override instance variable ? i do not understand. please explain.
 
Arno Reper
Ranch Hand
Posts: 286
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
yes you can override instance variable...


arno
[ April 17, 2006: Message edited by: Arno Reper ]
 
Balazs Borbely
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arno, Is seams that you didn't run the question to see the result.
The result is S1 even the object what calls the method is an S2 instance.

Why is this way?

Because the the attributes are linked at compile time. -> not enabled for polymorphism.
For the methods instead, the decision (which method to call) is taken at run-time. -> polymorphism enabled
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arno Reper:
hi,
yes you can override instance variable...


Actually, variables are not really "overriden," but instead can be "hidden." See JLS 8.3.3.1 and 8.3.3.2.
 
magesh sripathy
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Question13
{
String s = "outer";
public static void main(String args[])
{
S2 ob = new S2();
ob.display();
}
}
class S1
{
String s = "S1";
void display()
{
System.out.println(s);
}
}
class S2 extends S1
{
String s="S2";
}


As said by marc variaables are hidden and not overridden....
since the method called is in Class s1....the instance variable in that class is accesed..
suppose if the method display is in class s2,then the result will be "S2"....
 
Changchun Wang
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from jls4
The field that gets accessed is determined at compile time based on the type of the reference used to access it.

Inside a method, a reference to a field always refers to the field declared in the class in which the method is declared, or else to an inherited field if there is no declaration in that class.


hence the field s1 was refered
maybe you can comment this line //String s = "S1"; in class s1,and then a compile time error will occur
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Changchun,


from jls4 ....


Could you please post here the link where you get the JLS 4th Edition ? I thought the lastest version was 3rd Edition.

Thanks.
 
faisal usmani
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Actually, variables are not really "overriden," but instead can be "hidden." See JLS 8.3.3.1 and 8.3.3.2.


to add more , it is the member function that can be overridden , or we can Java supports runtime polymorphism for member functions , But it should not be static methods.
[ April 18, 2006: Message edited by: faisal usmani ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic