wood burning stoves 2.0*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "base casting" Watch "base casting" New topic
Author

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
Greenhorn

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

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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: base casting
 
Similar Threads
about protected static members
refs to static variables
Object Casting - Wyh is "i = 0"
mock questions from java prepare
Explanation needed