Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes extend class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "extend class." Watch "extend class." New topic
Author

extend class.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
Mike variables are resolved at compile time. U can't override the variables.
Hope this will help u.
vivek
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 03, 2000
Posts: 277
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 03, 2000
Posts: 277
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
 
Consider Paul's rocket mass heater.
 
subject: extend class.
 
Similar Threads
Problem on Polymorphism
emergency service required
Need Help
member data between subclass and base class
Inheritance concepts