aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why it displays wrongly?" Watch "why it displays wrongly?" New topic
Author

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"


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
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
Marshal

Joined: Mar 22, 2005
Posts: 41806
    
  62
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
Greenhorn

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

In Sub10
5


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: http://aspose.com/file-tools
 
subject: why it displays wrongly?