About Question enthuware.ocajp.i.v7.2.1390 :
Moderator: admin
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1390 :
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 ?
((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 ?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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.
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.
-
- Posts: 6
- Joined: Wed Apr 20, 2016 9:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
what would be the point of casting an object twice as mentioned in the explanation?
((Tone)(GoodSpeak)s).up();
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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.
-
- Posts: 7
- Joined: Sun May 08, 2016 8:09 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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:
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...
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
}
}
Now I'm totally stumped...
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
That is totally weird!That is simple
So I can cast ANY class to ANY interface, there are no rules at all and the compiler remains completely silent?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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.
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.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?
I suppose then, a class doesn't know wether it is subclassed or not, right?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
Right.JuergGogo wrote:Thank you, Paul.
I suppose then, a class doesn't know wether it is subclassed or not, right?
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Tue Nov 07, 2017 8:23 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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?
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?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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.
-
- Posts: 5
- Joined: Fri Apr 12, 2019 12:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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??
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??
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
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.
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.
-
- Posts: 5
- Joined: Fri Apr 12, 2019 12:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1390 :
Thank you so much
Who is online
Users browsing this forum: No registered users and 33 guests