• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Java exam question, casting

Here's a test question found on http://www.geocities.com/skmajji/Main.html
The anwer is a LIE! :-)  Why is the correct answer D.  Did the
program set the base clase object reference to the extended class?

Thanks

:    class Test
2:    {
3:       static void show()
4:       {
5:          System.out.println("Show method in Test class");
6:       }
7:    }
8:
9:    public class Q2 extends Test
10:   {
11:      static void show()
12:      {
13:          System.out.println("Show method in Q2 class");
14:      }
15:      public static void main(String[] args)
16:      {
17:           Test t = new Test();
18:           t.show();
19:           Q2 q = new Q2();
20:           q.show();
21:
22:           t = q;
23:           t.show();
24:
25:           q = t;
26:           q.show();
27:      }
28:  }

A) prints "Show method in Test class"
          "Show method in Q2 class"
          "Show method in Q2 class"
          "Show method in Q2 class"

B) prints "Show method in Test class"
          "Show method in Q2 class"
          "Show method in Test class"
          "Show method in Test class"

C) prints "Show method in Test class"
          "Show method in Q2 class"
          "Show method in Test class"
          "Show method in Q2 class"

D) Compilation error.
0
hank1
Asked:
hank1
  • 6
  • 4
  • 2
  • +1
1 Solution
 
zzynxSoftware engineerCommented:
>> Why is the correct answer D.
If it is, I think the compilation error you get will explain it ;°)
0
 
zzynxSoftware engineerCommented:
>> q = t;
Incompatible types
found   : Test
required: Q2

>> Why is the correct answer D
It's not because a Mercedes is a Car, that every Car is a Mercedes
Mercedes = Q2, Car = Test
0
 
bloodredsunCommented:
>>The anwer is a LIE! :-)

Says who?! Have a look in any IDE like Eclipse and it will tell you all you need to know.

Try to run it and then change  q = t; to q = (Q2) t; and try again
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hank1Author Commented:
I guess I assume when you did this

t = q;

they are now the same thing

later when

q = t

isn't that just saying Apples = Apples?
0
 
hank1Author Commented:
        if (t instanceof Test) System.out.println("T is a TEST object");
           if (t instanceof Q2) System.out.println("T is a Q2 object");

           t = q;
    if (t instanceof Test) System.out.println("after T is a TEST object");
           if (t instanceof Q2) System.out.println("after T is a Q2 object");

AFter the assignment t is a q2.
and this is true
if (t == q) System.out.println("Objects are the same!");
javac error:  (buy why ... they are the same?
Q2.java:32: incompatible types
found   : Test
required: Q2
          q = t;
              ^
1 error
0
 
TimYatesCommented:
They aren't the same...

Image "Test" is called "Humans" and "Q2" is called "Men"

Men are humans, so you can set a Human variable to contain a Man

Not all Humans are Men though, so you cannot store a Human object in a Man variable...

What if it were an instance of a woman?  Things would go horribly wrong (seen the Fly?) ;-)

Hope this goes some way to explain it...

Everyone else deserves the points for this Q more than me, as I am just repeating what they have said...

Tim
0
 
TimYatesCommented:
>> Image "Test" is called "Humans" and "Q2" is called "Men"

IMAGINE "Test" is called "Humans" and "Q2" is called "Men"
0
 
hank1Author Commented:
OK a different question which will help pound this past the rock here
in my skull.

Why is this true?

if (t == q) System.out.println("Objects are the same!");

0
 
TimYatesCommented:
that just means that both variables contain exactly the same object...

so assuming you have done:

  t = q ;

then both t and q contain the same object (an instance of Q2)
0
 
TimYatesCommented:
It's like you can do:

    Object o = new String( "Woot!" ) ;

but you cannot do:

    String o = new Object() ;

as String extends Object, so is considered more specific and so cannot contain the more general "Object" instance...

that's probably a good way of thining of it...

Imagine you have:

class Chest
{
    // get the weight of this object...
    public int getMass() ;
}

class Torso extends Chest
{
    // return the number of limbs
    public int numberofLimbs() ;
}

class EntireBody extends Torso
{
    // get the eyecolor
    public Color getEyeColor() ;
}

If you have:

Chest c = new Chest() ;

that's fine...

and

EntireBody b = new EntireBody() ;

is fine too...

and you can do:

c = b ;

as c is more general (and you can call getMass() on an EntireBody class)

but you can't do:

b = c ;

as c is a Chest object, so "getEyeColor()" makes no sense (and cannot be called) on b.

Hmmm...actually...that's kinda badly put too ;-)

hope it makes a bit of sense ;-)

Tim
0
 
hank1Author Commented:
So why the beef with ( since previously they did a t = q )
(ps - would this also schedule the now defunct t object
for garbage collection?)

q = t;  ?  They are the same.  Right?

The compiler want

q = (Q2)t;  Why?

ps - thanks for suffering a knuckle head.  I still don't get this.  :-)
0
 
TimYatesCommented:
>> q = t;  ?  They are the same.  Right?

At run time, yes...  the compiler isn't good enough to pick that up though...  I don't think any compiler is good enough...

Syntactically, they could be incompatable objects...  it just so happens that at runtime, you make them the same object...

>> The compiler want
>> q = (Q2)t;  Why?

Because by casting it, you are telling the compiler to shut up, that you know what you are doing, and to trust you that you won't do anything bizarre...

if t is incompatable with q, you will then get a ClassCastException at runtime
0
 
TimYatesCommented:
>> ps - thanks for suffering a knuckle head.  I still don't get this.  :-)

No worries...  it's kinda hard to explain ;-)  but it will click, believe me :-D
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now