• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

class cast -- confused  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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: 42975
76
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23569
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Posts: 23569
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!