• 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

class cast -- confused

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you observe above code there is no heriracy bitween M and A. But, #1 is compiled successfully. But #2 is giving compilation error.

Please provide explanation.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cast of m to an A is possible because an object that implements M could conceivable be an A (m could point to an instance of a subclass of A that *does* implement M). Remember that a cast does not mean "dear compiler, make sure it works" but instead "dear compiler, accept this, because I'm telling you that it will work".

Whereas a D can never be cast to an A, as you correctly point out.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface M{
}
class A
{
};
class D implements M{
}

Why a semicolon is used after the class A?
Is it a typo error?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Narendhiran Nagarajan wrote:
Why a semicolon is used after the class A?
Is it a typo error?



Well, it may be a typo... but it is definitely *not* an error. Semicolons are optional after a class definition.

Henry
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hehe, C++ behaviour - adding semicolons after class definition
 
Lukas Sieradzki
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:m could point to an instance of a subclass of A that *does* implement M)


But A has no subclasses! I don't understand your explanation.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mason Storm wrote:

Ulf Dittmer wrote:m could point to an instance of a subclass of A that *does* implement M)


But A has no subclasses! I don't understand your explanation.




Well, you know that... but how does the compiler know that?

How will the compiler know that you won't write a subclass sometime in the future, which implements the interface?

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic