• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
Tanatep Pichetvasin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prashanti Mukund
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , i got it
 
Tanatep Pichetvasin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your explaination
But why doesn't Java override the attribute ? Why override for only method ?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Tanatep Pichetvasin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja, Thank you your answer
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett, thanks for the interesting tidbit!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic