All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Mon Aug 10, 2015 3:27 pm 
Offline

Joined: Thu Feb 19, 2015 8:25 am
Posts: 21
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 ?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Mon Aug 10, 2015 7:43 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
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.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Sat Apr 23, 2016 5:00 am 
Offline

Joined: Wed Apr 20, 2016 9:56 am
Posts: 6
what would be the point of casting an object twice as mentioned in the explanation?
Quote:
((Tone)(GoodSpeak)s).up();


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Sat Apr 23, 2016 9:33 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
There is no point. It is a purely theoretical exercise :)


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Thu May 26, 2016 1:35 pm 
Offline

Joined: Sun May 08, 2016 8:09 am
Posts: 7
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:
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... :?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Thu May 26, 2016 11:16 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
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.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Thu Oct 12, 2017 3:12 pm 
Offline

Joined: Mon Sep 25, 2017 8:16 am
Posts: 29
Quote:
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?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Thu Oct 12, 2017 8:55 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
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.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Fri Oct 13, 2017 1:24 am 
Offline

Joined: Mon Sep 25, 2017 8:16 am
Posts: 29
Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1390 :
PostPosted: Fri Oct 13, 2017 9:39 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
JuergGogo wrote:
Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?

Right.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


Who is online

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


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum