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

Moderator: admin

Sergiy Romankov
Posts: 21
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post by Sergiy Romankov » Mon Aug 10, 2015 3:27 pm

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 ?

admin
Site Admin
Posts: 7326
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Aug 10, 2015 7:43 pm

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.

Jokumo♫
Posts: 6
Joined: Wed Apr 20, 2016 9:56 am
Contact:

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

Post by Jokumo♫ » Sat Apr 23, 2016 5:00 am

what would be the point of casting an object twice as mentioned in the explanation?
((Tone)(GoodSpeak)s).up();

admin
Site Admin
Posts: 7326
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Apr 23, 2016 9:33 am

There is no point. It is a purely theoretical exercise :)

Ingvarr
Posts: 7
Joined: Sun May 08, 2016 8:09 am
Contact:

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

Post by Ingvarr » Thu May 26, 2016 1:35 pm

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... :?

admin
Site Admin
Posts: 7326
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Thu May 26, 2016 11:16 pm

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.

JuergGogo
Posts: 29
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

Post by JuergGogo » Thu Oct 12, 2017 3:12 pm

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?

admin
Site Admin
Posts: 7326
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Thu Oct 12, 2017 8:55 pm

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.

JuergGogo
Posts: 29
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

Post by JuergGogo » Fri Oct 13, 2017 1:24 am

Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?

admin
Site Admin
Posts: 7326
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Oct 13, 2017 9:39 pm

JuergGogo wrote:Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?
Right.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests