This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes base casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "base casting" Watch "base casting" New topic

base casting

Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
why is the answer i=1 and j=2.
I mean j=2 is fine but how can i be equal to 1 it should be 0, I think.
please explain, appreciate it!
class A {
protected int mv;
protected int getMv() {
int mv = 1;
return mv;
class B extends Class A {
public int mv;
protected int getMv() {
int mv = 2;
return mv;
static public void main(String s[]) {
B b = new B();
int i = ((A)b).mv;
int j = ((A)b).getMv();
System.out.println("i="+i+", j="+j);

~-<br />Doing my best this time and everytime.<br />-- Me
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Subsh:
For me following code printed i = 0 and j = 2. I had to make couple of correction in the code. For next time, it will be easier if you paste your actual code that is printing what you saying it is printing. Even better if you paste your code using code button below. That I do not have to spend time indenting your code
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
Yes you are right. The program did print i=0,j=2. Shubh, you should send an email to the person who has written the question to correct his mistake.

_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
sun moon

Joined: Oct 09, 2002
Posts: 28
can pls explain me what is ((A)b) signifies above
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
(A)b casts b (of type B) to type A.

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: base casting
It's not a secret anymore!