Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Programmer Certification (OCPJP)
Variable hiding
lu v thuan
Ranch Hand
Posts: 32
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Friends
class A{ final int i = 5; } class B extends A{ int i = 10; public static void main(String arg[]){ B bobj = new B(); bobj.i = 15; System.out.println(bobj.i); System.out.println( ((A)bobj).i); } }
I think the result is :15 , 15
Am i wrong, if so pls make it clear to me?
Thank in advanced
Anonymous
Ranch Hand
Posts: 18944
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The answer is 15 and 5.
The cast you have made here (
(A)bobj)
)is equivalent to assigning a base class reference to a superclass type. Hence the i in the superclass gets invoked. Do take a look at this modified code and run it...it might help.
<font size=2> class A{ final int i = 5; } class B extends A{ int i = 10; public static void main(String arg[]){ B bobj = new B(); bobj.i = 15; System.out.println(bobj.i); // System.out.println( ((A)bobj).i); A aobj = bobj; System.out.println(aobj.i); } }
HTH
Shyam
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
can parent class constructor be invoked using this operator?
Why the output is 2 here?
Protected in subclasses
How to write generic method for this method?
constructor invocation order/priority
More...