This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple casting

 
John King
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it?
 
John King
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Have you tried it?


I do not have a computer with me.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works fine...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John King:

I do not have a computer with me.


And you posted this using...?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
... And you posted this using...?

Doh! That never even occurred to me.
 
John King
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20187
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic