Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question About Statement: B x = new A();

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is a mock exam question and the answer appears at the bottom. I'm confused by the following statement:
B x = new A();
It seems that this is constructing a new object called 'x' that is of type A. Why should there be a return type of B? I'd expect this statement to invoke the constructor in class A which is as follows:
A(){x = 20;}
Since x is assigned a value of 20 inside the constructor, I'd expect that just as with any other method the class variable x in class A is not changed to 20 but rather keeps its default value of zero. I'd therefore a expect a zero to be printed by the first println() statement which is as follows.
System.out.println(x.x);

Instead, the answer below indicates that the first println() statement prints out a 10. Could someone please clarify this for me? Thanks very much.
---------------------------
Exam Question: Give the output of this program.
class B{
int x;
B(){ x = 10;}
void m1(){x = 20;}
void m1(int x){this.x = x;}
}
class A extends B{
int x;
A(){x = 20;}
void m1(int x){super.x = x;}
public static void main(String arf[]){
B x = new A();
System.out.println(x.x);
x.m1();
System.out.println(x.x);
x.m1(30);
System.out.println(x.x);
((A)x).m1(30);
System.out.println(x.x);
}
}
Answer: 10 20 30 30
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!
I'm going to try to point you in the direction that will help you answer the questions you have...
First,
B x = new A(); is the polymorphism in action --
where the REFERENCE variable and the OBJECT are of different, but related types. They are related in that the OBJECT type must be either a subclass OR an implementation class of the REFERENCE type.
Polymorphically, if you invoke a method on the REFERENCE x, the actual OBJECT on the heap runs the method, so if the object has overridden the method (as the A class does with the overloaded m1(int x) method), then it is the actual OBJECT type that runs the method, rather than the REFERENCE type.
If you invoke m1(int x) on the B reference, the A object's version actually runs.
But... this is NOT the case with variables. Variables are NOT polymorphic. If you ask a B REFERENCE for its 'x' variable, you will get the B version of x, even though it is really an A object out on the heap! Java looks only at the REFERENCE type when deciding which variable to use. It does not look to see which actual OBJECT is really being referenced.
The other quirky thing about this example (which I really like, by the way) is that even though there is only ONE object, of type A, on the heap -- the object has several 'layers'. It has its "A" parts and its "B" parts (and its Object parts, but we don't care about that now).
So the A part of the object has an 'x' variable, and the B part of the object ALSO has an 'x' variable.
But the bottom line is, variables are not polymorphic, so it is the REFERENCE type that determines whether the A or B version of 'x' is used. With methods, of course, this is completely different. Non-static methods are *always* polymorphic, so it is always the OBJECT type (regardless of the reference type) that determines which version of the overridden method will run.
(and the A constructor certainly *does* run, as does B's constructor -- in the chain, when you say 'new A()' the A constructor is invoked, which in turn invokes the B constructor, which in turn invokes the Object constructor. )
So look at the question again and see if you can make it work... (or I might have just made it even more confusing
cheers,
Kathy
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy,
You truly have a knack for describing Java concepts in understandable terms. Thanks so much for providing such a concise explanation.
Michael
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic