• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

A very confusable question?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Demo{
public static void main(String[] args){
(new SSC()).demo();
}
}
class C{
double value=300.00;
public double getvalue(){
System.out.println("C.value="+value);
return value;
}
}
class SC extends C{
double value=400.00;
public double getvalue(){
System.out.println("SC.value="+value);
return value;
}
}
class SSC extends SC{
double value=500.00;
public double getvalue(){
System.out.println("SSC.value="+value);
return value;
}
public void demo(){
getvalue();
((SC)this).getvalue();
((C)this).getvalue();
System.out.println();
System.out.println(value);
System.out.println(((SC)this).value);
System.out.println(((C)this).value);
}
}
the result is:
SSC.value=500.00
SSC.value=500.00
SSC.value=500.00
500.00
400.00
300.00
The reason is: method binds to a object at runntime
and variable binds depend on the object type at compiletime;
But i think just because of this reason,the answer is
SSC.value=500.00
SC.value=400.00
C.value=300.00
500.00
500.00
500.00
.
So what is the "(SC)this" really refers to at compiletime and runtime?
Is the result means that "(SC)this" acts at compiletime?
[This message has been edited by theMatrix (edited June 18, 2001).]
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Matrix,
Method demo() is invoked by a SSC-object.
So demo()'s getvalue()-method retrieves value = 500.00 as
stated in class SSC.
What you do in method demo() is a lot of casting e.g.
System.uot.println((SC)this)).value
casts SSC-Object to datatype SC (Superclass of SSC).
When you cast, the value of the instance variables are
not changed but only the datatype of the object from SSC to SC.
As far as I know there won't be a new initialisation of SC
object so that value isn't initialized with 400 (SC initialsation of "value).
Please correct me if I'm wrong.
Thomas.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
theMatrix
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at "JavaRanch Naming Policy" . We require names to have at least two words (not just letters), separated by a space, and strongly recommend that you use your full real name. We want to get to know you as a Professional. Please choose a new name which meets the requirements and re-register.
Cindy
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,
I told you something wrong.
class A {
i=10;
...
}
class B extends A
i=20;
...
}
A b = new B(); //B is subclass of A = Cast
System.out.println(b.i);
b is an B-object with an A reference. Therefore it will take
the methods of B and the variables from A.
This means that b.i will print i=10;
Thomas
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic