• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

multiple casting

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it?
 
John King
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works fine...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic