my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another Question." Watch "another Question." New topic
Author

another Question.

Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
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";
}


Amisha Shah.<br />SCJP 1.4
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
hi,
because you override the instance variable s and the display method print the s that's defined into the S2 object
arno


The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
hi arno,

can i override instance variable ? i do not understand. please explain.
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
hi,
yes you can override instance variable...


arno
[ April 17, 2006: Message edited by: Arno Reper ]
Balazs Borbely
Ranch Hand

Joined: Oct 11, 2004
Posts: 33
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


'Make everything as simple as possible, but not simpler.' --Albert Einstein
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
magesh sripathy
Greenhorn

Joined: Apr 06, 2006
Posts: 27
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

Joined: Feb 15, 2006
Posts: 83
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

Joined: Jan 19, 2006
Posts: 584
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.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another Question.