| Author |
Polymorphism
|
Tanatep Pichetvasin
Greenhorn
Joined: Aug 26, 2008
Posts: 3
|
|
From the below code, the class B is the subclass of class A. After the instance a is created, the method go is called. Java run this method of the class B. And the value of the attribute x is changed within the method go. Then I display the value of the attribute x of a. The initial value of x in class A is shown. My question is why doesn't Java show the lastest value of x after the method go finish ? output >> B.go() : x = 600 x = 10 ---------------------------- B.go() : x = 600 x = 600 [edit]Add code tags. CR[/edit] [ August 27, 2008: Message edited by: Campbell Ritchie ]
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Originally posted by Tanatep Pichetvasin: import java.io.*; class A { public int x = 10; void go() throws IOException { x *= 10; System.out.println("A.go() : x = " + x); } } class B extends A { public int x = 20; void go() throws EOFException { x *= 30; System.out.println("B.go() : x = " + x); } void gone() {} }
the overridding method should not throw new checked exception.it should give compilation error. Hope This Helps
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
the overridding method should not throw new checked exception.it should give compilation error.
EOFExeption extends IOException, so no compiler error. Instance variables accessed by the reference type. That's why a.X output 10. hope this helps
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Prashanti Mukund
Greenhorn
Joined: Aug 09, 2008
Posts: 4
|
|
In the code you are declaring A's instance of type B so when you call a.go the control is passed to B's go method as a result A's x value is not changed try creating A's instance as and then call method now a will return 100 not 10 hope you understood
|
 |
Prashanti Mukund
Greenhorn
Joined: Aug 09, 2008
Posts: 4
|
|
|
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
Morning, and welcome to JavaRanch both of you Tanatep Pichetvasin: please find the CODE button for posts in future; I have edited your post to add code tags, and you can see how they make the text easier to read.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Thanks , i got it
|
 |
Tanatep Pichetvasin
Greenhorn
Joined: Aug 26, 2008
Posts: 3
|
|
Thank for your explaination But why doesn't Java override the attribute ? Why override for only method ?
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
The simple answer is: because that's the way it is specified. Now, why has it been specified that way? I guess there are mainly two reasons: - in a good OO design, data should be private, anyway, in which case polymorphism isn't needed, and - making fields polymorphic would make the JVM more complex and could lead to decreased performance. As an aside, i don't know of any OO language where fields are polymorphic. So another reason could be tradition...
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Tanatep Pichetvasin
Greenhorn
Joined: Aug 26, 2008
Posts: 3
|
|
Ilja, Thank you your answer
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Originally posted by Ilja Preuss: As an aside, i don't know of any OO language where fields are polymorphic. So another reason could be tradition...
Fields in Scala are polymorphic. Actually, from the clients perspective there's no difference between fields and methods. Fields in a subclass can override no-argument method declarations in a parent class or trait. But Scala is a multi-paradigm language so maybe this doesn't come from the OO side of it's family. [ August 28, 2008: Message edited by: Garrett Rowe ]
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Garrett, thanks for the interesting tidbit!
|
 |
 |
|
|
subject: Polymorphism
|
|
|