Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Mon Aug 10, 2015 3:27 pm
by Sergiy Romankov
with ((GoodSpeak)speak).tuneUp(); is clear here we use downcating to get method which is declared in class GoodSpeak, but I don`t exactly understand how it works with
((Tone)speak).tuneUp();
speak holds object of GoodSpeak class, but reference is of type Speak which is not declared
method tuneUp, we make casting to Tone actualy upcasting which has declared abstract method tuneUp(), but how we call exactly method of class GoodSpeak I don`t get it ?

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Mon Aug 10, 2015 7:43 pm
by admin
You have to remember only one simple and fundamental concept.
Instance methods are invoked on the actual object referred to by a reference. Compiler doesn't know the actual type of the object (because objects are created at run time by the JVM). So no matter what is the type of the reference, it is the type of the actual object at run time whose method will be invoked. The compile can only check if such a method is available in the type of the reference. So when you cast speak to (Tone), the compiler checks if tuneUp method is available in Tone. If it is not available in that type, it will fail to compile.

Here, speak refers to an object of class GoodSpeak, so GoodSpeak's tuneUp will be invoked.

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Sat Apr 23, 2016 5:00 am
by Jokumo♫
what would be the point of casting an object twice as mentioned in the explanation?
((Tone)(GoodSpeak)s).up();

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Sat Apr 23, 2016 9:33 am
by admin
There is no point. It is a purely theoretical exercise :)

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Thu May 26, 2016 1:35 pm
by Ingvarr
I'm confused as to why it is ever possible to cast s to Tone, as in Option 1, ((Tone)s).up();
While ((Tone)(GoodSpeak)s).up(); seems logical, Tone and Speak appear unrelated, at least in my eyes. If Tone were a class, the compiler would've complained bitterly. So why does it keep silent when it comes to interfaces?
Perhaps, I can rephrase my questions with this:

Code: Select all

interface I {  void implementMe(); }
class A {}

class MyClass {
    public static void main(String[] args) {
        MyClass mc = new MyClass();
        System.out.println(     (I)mc    );      // compiles just fine
//        System.out.println(   (A)mc    );      // INVALID
    }
}
Naturally, (I)mc will throw a CCE at runtime but that's irrelevant. The point is why on earth the compiler is happy to cast a reference var to the interface type that remains unimplemented and, therefore, is a complete stranger to this particular class?

Now I'm totally stumped... :?

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Thu May 26, 2016 11:16 pm
by admin
That is simple :) An reference variable can easily refer to an object of a subclass (which may even be unknown to the compile at compile time) and it is possible that the subclass does implement the interface. So the compiler has no choice but to allow it to compile and leave it to the JVM to worry about it at run time.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Thu Oct 12, 2017 3:12 pm
by JuergGogo
That is simple :)
That is totally weird!
So I can cast ANY class to ANY interface, there are no rules at all and the compiler remains completely silent?

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Thu Oct 12, 2017 8:55 pm
by admin
No, you don't cast a class to anything. You cast a reference to another class or interface.
Yes, you can cast any reference to any interface unless of course the reference is declared to be of a class that is final.

It is not weird but very logical as explained above.

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Fri Oct 13, 2017 1:24 am
by JuergGogo
Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Fri Oct 13, 2017 9:39 pm
by admin
JuergGogo wrote:Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?
Right.

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Thu Mar 08, 2018 1:40 pm
by SeoaneR
The questions asks : What can be inserted in the code below so that it will print UP UP UP?

In relation to ((Tone)s).up();
I get a ClassCastException so their is no print output of UP UP UP.
So why is the answer correct?

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Thu Mar 08, 2018 9:25 pm
by admin
Do you have "implements Tone" in GoodSpeak? Please make sure that you have typed the code exactly as given in the question.

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Sun Jul 21, 2019 4:41 pm
by chiran12345
I'm confused, why it is possible to cast s to Tone, ((Tone)s).up();

Per my understanding, we can use parent reference to hold child object and can use interface to hold implemented class object. I used 3 fundamental points to remember in Object Type casting: lets say

A b =(C) d; where A and C= class/interface, and b= name of reference variable, d = reference variable name

1 (compile time checking): the type of d and C must have relation either child to parent vice versa or same type. Otherwise we will get compile time error (CE) saying inconvertible types.

2 (compile time checking): C must be either same or derived type of A, otherwise CE saying incompatible types

3(Runitme Checking): Runtime Object type d must be either same or derived type of C, otherwise RE saying ClassCastException.


So, in our case, s and Tone doesn't have relation, why it is not compile time error? Please enlighten me??

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Sun Jul 21, 2019 8:44 pm
by admin
Point 1 in your list only talks about classes not interface.
In case of an interface, the compiler checks whether it is possible for a reference to point to an object that implements the interface to which it is being casts. So, when you do ((Tone)s).up();, the compiler checks whether s can point to an object of a class that implements Tone. Here, it is possible because type of s is Speak and it is not a final class. So, s can point to a subclass of Speak that implements Tone.

Re: About Question enthuware.ocajp.i.v7.2.1390 :

Posted: Mon Jul 22, 2019 7:48 am
by chiran12345
Thank you so much