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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post 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 ?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post by Jokumo♫ »

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

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

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

Post by admin »

There is no point. It is a purely theoretical exercise :)
If you like our products and services, please help us by posting your review here.

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

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

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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post by JuergGogo »

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

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

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

Post by admin »

JuergGogo wrote:Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?
Right.
If you like our products and services, please help us by posting your review here.

SeoaneR
Posts: 8
Joined: Tue Nov 07, 2017 8:23 am
Contact:

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

Post 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?

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

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

Post by admin »

Do you have "implements Tone" in GoodSpeak? Please make sure that you have typed the code exactly as given in the question.
If you like our products and services, please help us by posting your review here.

chiran12345
Posts: 5
Joined: Fri Apr 12, 2019 12:00 pm
Contact:

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

Post 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??

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

chiran12345
Posts: 5
Joined: Fri Apr 12, 2019 12:00 pm
Contact:

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

Post by chiran12345 »

Thank you so much

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests