Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Polymorphism

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks , i got it
 
Tanatep Pichetvasin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank for your explaination
But why doesn't Java override the attribute ? Why override for only method ?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja, Thank you your answer
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garrett, thanks for the interesting tidbit!
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic