Link to home
Start Free TrialLog in
Avatar of hank1
hank1

asked on

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.
Avatar of zzynx
zzynx
Flag of Belgium image

>> Why is the correct answer D.
If it is, I think the compilation error you get will explain it ;°)
>> 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
>>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
Avatar of hank1
hank1

ASKER

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?
Avatar of hank1

ASKER

        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
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
>> Image "Test" is called "Humans" and "Q2" is called "Men"

IMAGINE "Test" is called "Humans" and "Q2" is called "Men"
Avatar of hank1

ASKER

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!");

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)
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
Avatar of hank1

ASKER

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.  :-)
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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