• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

extend class.

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
what's the input for the following program? the answer is "The value is 2 The value is 3. I thought it should be "The value is 2 The value is 2". but when I ran it, it proved i was wrong. is it polymorpism? Can someone give me detailed explanation? I am really appreciated!
class Base {
int x=3;
public Base() {}
public void show() {
System.out.print(" The value is " + x);
}
}
class Derived extends Base {
int x=2;

public Derived() {}
public void show() {
System.out.print(" The value is " + x);
}
}
public class Test1 {
public static void main(String args[]) {
Base b = new Derived();
b.show();
System.out.println("The value is " +b.x);
}
} // end of class Test

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Mike variables are resolved at compile time. U can't override the variables.
Hope this will help u.
vivek
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
I want to know what's the difference between
"Base b1 = new Derived" and "Derived b2 = new Derived".
I think both b1 and b2 are reference to the object of Derived. But why b1.x and b2.x are different?
mike
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mike,
Let me try to explain it.
It is not known until runtime that b1 and b2 both refer to the same object. At compile time b1 refer to a object of Base class (since b1 is declared as type of Base class) and b2(since b2 is declared as type of Derived class) refer to object of Derived class. Since the variables are resolved only at compile time so they are resolved according to their declaration type not according to their actual type.
Hope I can express myself and help u.
Please feel free to correct me.
vivek

[This message has been edited by Vivek Shrivastava (edited July 26, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek,
if the b1 and b2 refer to same object until run time. so they both should have same value of x when the program runs, but I still cannot understand why I got different result when I ran the program with b1 and b2.
when using b1, the result is "The value is 2 The value is 3"
when using b2, the result is "The value is 2 The value is 2"
Obviously, they refer to different object. Could you please help me clear my doubt on this question? Also if I want to use b1 to print out "The value is 2 The value is 2", how can i change the code?
Thank you!
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mike,
Let me try once again, i think i could not explain it properly
Base b1 = new Derived()
Derived b2 = new Derived()
At compile time compiler does not know that b1 is a refrence to a object of Derived type, because it use the declare type of the refrence variable and b1 is declared as a refrence to object of type Base. But at run time it finds that b1 is actually a refrence to a object of Derived type.
But in case of b2 at compile time compiler does know that b1 is a refrence to a object of Derived type, because b1 is declared as a refrence to object of type Derived.
Since the variables are resolved only at compile time so they are resolved according to their declaration type not according to their actual type. so at compile time b1 is a refrence to object of Base type. that is why it prints '3'.
But if u want to print "The value is 2 The value is 2" using b1 cgange it to
Derived b1 = new Derived()
Hope I can express myself and help u.
Please feel free to correct me.
vivek
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic