aspose file tools*
The moose likes Java in General and the fly likes Interface and typecasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface and typecasting" Watch "Interface and typecasting" New topic
Author

Interface and typecasting

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
interface Colorable1 {}
class Vehicle1 {}
class bike1{}

public class Pool {

public static void main(String[] args) {
Vehicle1 b = new Vehicle1();
Colorable1 j = (Colorable1) b; -----> Compiles Fine
bike1 k = (bike1) b; -----> Compiler Error
}
}

In the above program Vehicle1 doesnot have any relation with Colorable1(Interface) and bike1(class), i.e it does not belong to same inheritance hierarchy

But i am able to typecast from Vehicle1 to Colorable1 even though there is no inheritance hierarchy ---> How
When i typecast from Vehicle1 to bike1 am getting compiler error, since they are not in inheritance hierarchy...

Why and how did that interface stuffs work?? Why not the class bike1 didnt work??
Can any body give suggestion on this please???
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.coderanch.com/t/503471/java-programmer-SCJP/certification/Reference-Type-Casting
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Could you please explain it briefly please...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
A bike is a vehicle but a vehicle might not be a bike.
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84

But there is no inheritance in the example... Bike is not inherited from Vehicle in the example

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Naresh Shanmugam wrote:
Why and how did that interface stuffs work?? Why not the class bike1 didnt work??
Can any body give suggestion on this please???


There may be a situation, that any sub classes of class implements that interface, so in java, type casting to an interface is allowed. But for class, if those classes were not in the same hierarchy, the Compiler definitely flag an Error. For interfaces, it can't sure this.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Naresh Shanmugam wrote:
But there is no inheritance in the example... Bike is not inherited from Vehicle in the example

I didn't notice that, sorry. In which case it is obvious to the compiler that this cast will fail, so it throws an error.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Abimaran Kugathasan wrote: . . . For interfaces, it can't sure this.
Have look at the Java™ Language Specification; there is bound to be something useful there.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Campbell Ritchie wrote:
Abimaran Kugathasan wrote: . . . For interfaces, it can't sure this.
Have look at the Java™ Language Specification; there is bound to be something useful there.


Could you Please spot the problem here? I couldn't get, what you are trying to tell!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Look through the JLS as quoted and see whether you can find anything about casting or type conversions. You can easily find its index from the link I gave you.
Jatin Dhingra
Greenhorn

Joined: Jun 28, 2010
Posts: 29
As i understand it...Vehicle1 and bike1 are classes and are not related, so you cant typecast one to other.
BUT you can typecast Vehicle1 to Colorable1 because Colorable1 is interface and in view of compiler, there can be class at runtime that inherits Vehicle1 and implements Colorable1. So in compiler's view, This is legal. ofcourse if this is not the case, it will cause run time error( as it will be in your program).

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

How can you create a class at run-time , and that too implementing that interface?? Is it really possible, and the compiler thinks, you might possibly do it?


OCPJP 6
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Vinoth Kumar Kannan wrote:How can you create a class at run-time , and that too implementing that interface?? Is it really possible, and the compiler thinks, you might possibly do it?


I think its not runtime. If you create a class xyz which extends Vehicle1 and implement Colorable1, then the type casting is valid.
Otherwise you will get a runtime exception.


SCJP 5, SCWCD 5
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Even if,


I very well accept these are possible!

How is this possible at all?? Isn't Vehicle1 and Colorable1 independent.
We are trying to give the Colorable's view of xyz to Vehicle1, but, Vehicle1 class has no idea about the methods in the Colorable interface as it does not implement it. Also, it will not have any idea that its subclass may implement this interface.
Can anyone explain this concept, please......
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vinoth Kumar Kannan wrote:

How is this possible at all?? Isn't Vehicle1 and Colorable1 independent.
We are trying to give the Colorable's view of xyz to Vehicle1, but, Vehicle1 class has no idea about the methods in the Colorable interface as it does not implement it. Also, it will not have any idea that its subclass may implement this interface.
Can anyone explain this concept, please......


OK, Did you go through the link, which Campbell Ritchie gave?
If S is an interface type, and If T is a class type that is not final, then the cast is always correct at compile time (because even if T does not implement S, a subclass of T might)


So, make your Vehicle class as final class, and do the casting as you provided, then check the compilation. You'll get it. There may be possibility, that any of the sub classes of the Vehicle class implements this interface, so for that sub class' object, it can be referenced by Vehicle reference variable and Colorable reference variable, but actual object is from that sub class. So it can up caste to a super class(in your case, Vehicle).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Abimaran Kugathasan wrote: . . . make your Vehicle class as final class, . . . sub classes of the Vehicle class . . .
Can you see the contradiction in what you wrote?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Campbell Ritchie wrote:
Abimaran Kugathasan wrote: . . . make your Vehicle class as final class, . . . sub classes of the Vehicle class . . .
Can you see the contradiction in what you wrote?


Those are not related to each other,
Abimaran Kugathasan wrote:
There may be possibility, that any of the sub classes of the Vehicle class implements this interface......


This is for the case, which he defined(Vehicle is not final class). If he defined Vehicle as final class, then he can't do this kind of casting. This is the think, I want to tell him.
Jatin Dhingra
Greenhorn

Joined: Jun 28, 2010
Posts: 29
Vinoth Kumar Kannan wrote:Even if,


I very well accept these are possible!

How is this possible at all?? Isn't Vehicle1 and Colorable1 independent.
We are trying to give the Colorable's view of xyz to Vehicle1, but, Vehicle1 class has no idea about the methods in the Colorable interface as it does not implement it. Also, it will not have any idea that its subclass may implement this interface.
Can anyone explain this concept, please......


" Vehicle1 vehicle=(Colorable1)new xyz();" is not right, This gives compiler error when compiled.
You are type casting new object of class xyz to Colorable1 type. You can assign it only to variable of type Colorable1.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

sorry that must have been

I was about to ask, how compiler does not throw an error on this, if

exists though.
I still strongly feel, Colorable and Vehicle1 are independent.
Also,isn't it -A subclass depends on a superclass?
Somebody said, if a subclass of Vehicle1 implements Colorable, then Vehicle1 can be cast with Colorable. - This makes: 'A super class depends on its subclass'
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vinoth Kumar Kannan wrote:sorry that must have been



Did you check it? It should give compilation error, those are not compatible types!
Jatin Dhingra
Greenhorn

Joined: Jun 28, 2010
Posts: 29
Vinoth Kumar Kannan wrote:sorry that must have been

I was about to ask, how compiler does not throw an error on this, if

exists though.
I still strongly feel, Colorable and Vehicle1 are independent.
Also,isn't it -A subclass depends on a superclass?
Somebody said, if a subclass of Vehicle1 implements Colorable, then Vehicle1 can be cast with Colorable. - This makes: 'A super class depends on its subclass'


Relationship between superclass and subclass is "IS A" not depends on. See corey's article http://radio.javaranch.com/corey/2004/04/05/1081179854000.html
this might be helpful.

 
jQuery in Action, 2nd edition
 
subject: Interface and typecasting