This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why it displays wrongly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why it displays wrongly?" Watch "why it displays wrongly?" New topic

why it displays wrongly?

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689

why it displays "in sub and 5"
why not "in sub and 10"

SCJP 1.4
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
why it displays "in sub and 5"
why not "in sub and 10"

Because polymorphism does not apply to variables.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41155
It does display "In Sub10", and then "5" on the next line, which is as it should be. Where did you see it displaying "in sub and 5"?

Ping & DNS - my free Android networking tools app
Sureshkumar Chinnappan

Joined: Feb 15, 2006
Posts: 27
The output is

In Sub10

Warm Regards,<br />Suresh Kumar<br /> <br />SCJP 5.0<br />SCWCD 1.4
Rohit Suman
Ranch Hand

Joined: Sep 09, 2006
Posts: 37
hi the answer to your question is that while accessing the fields the
reference of is of superclass type so it access the filed of superclass and prints 5 but when you are calling the method the object is of subclass so the method of subclass is called and since there is implicit this reference so the field of subclass is being accessed and is printing 10
hope it clears your doubt
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
fields are invoked depending on reference whereas mehtods are invoked depending on objects.

hope you got it.
I agree. Here's the link:
subject: why it displays wrongly?
Similar Threads
Differance in overriding variable and method
Class/Sub Class
Please explain me the flow of this simple prog.......
Please explain(about accessing members)!
need help