aspose file tools*
The moose likes Beginning Java and the fly likes multiple casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multiple casting" Watch "multiple casting" New topic
Author

multiple casting

John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
Multiple down casting

I have a base class called BaseClass and a base interface BaseInterface.

class MyClass expends BaseClass implements BaseInterface

After I create an instance of MyClass called myClass, can I do this
casting:

BaseClass baseClass = (BaseClass) ((BaseInterface) myClass));
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Have you tried it?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
Originally posted by marc weber:
Have you tried it?


I do not have a computer with me.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This works fine...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by John King:

I do not have a computer with me.


And you posted this using...?


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ernest Friedman-Hill:
... And you posted this using...?

Doh! That never even occurred to me.
John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
Originally posted by Ernest Friedman-Hill:


And you posted this using...?


I was using a terminal that only allows me to browse the Internet in a public place. Thanks guys.

I tested it. As Marc said, it compiles fine. It was an interview question.
I do not understand why Java allows this type of casting. It seems not very useful.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

There are hundreds of ways to write useless and pointless code in Java. But trying to write a language definition that makes code like "i = i++" illegal because it doesn't do anything and does it in a confusing way, for example, would be extremely difficult. It's better to just write a simple language and let programmers do what they will.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Paul Clapham:
... It's better to just write a simple language and let programmers do what they will.

I agree with the first part.
John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
Originally posted by Paul Clapham:
But trying to write a language definition that makes code like "i = i++" because it doesn't do anything .


You brought an interesting point in Java. I like to summarize this:

int i=10;

i=i++; // i is still 10
i=i--; // i = 10.

i += i++; // it gives i=20

i=10;
i += ++i; // it gives i=21

i=10;
i -= i++; // it gives i=0

i=10;
i -= --i; // it gives i=1

i=10;
i += i--; // it gives i=20
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple casting