This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Cannot cast to an derived class

 
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
original code:


Now I derive a class from BarRenderer and override some of its method

How come it is not allowed, barRenderer is just a pointer, it should be allowed to cast to
an object of a derived class. Or no?
I need to override BarRenderer of JFreeChart in order to get it animated.
Thanks
Jack
 
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A reference is just a reference. If the object is refers to is a myBarRenderer, then the cast is allowed, if not, as apparently in this case, it will not work and will lead to a run-time exception.

If it was not designed to be extendable, then It is unlikely that JFreeChart knows about the class "myBarRenderer", how should it return an instance thereof?

 
Ivan Jozsef Balazs
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it should be allowed to cast to an object of a derived class. Or no?



It is the other way around: an instance of a derived class is per definition an instance of the base class, but then there is no need for casting.
An instance of the base class is either an instance of a derived class or not.

A Cat instance is an Animal instance for sure, but an Animal instance is not forcibly a Cat instance it can be a Dog etc.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:

it should be allowed to cast to an object of a derived class. Or no?



It is the other way around: an instance of a derived class is per definition an instance of the base class, but then there is no need for casting.
An instance of the base class is either an instance of a derived class or not.

A Cat instance is an Animal instance for sure, but an Animal instance is not forcibly a Cat instance it can be a Dog etc.



Wonder what is the correct way of doing it?
There are methods like drawItem that draws a bar of a chart with a plot.
I override this to include an incremental value to offset the x,y coordinates of the bar, so that by timer events, I can
gradually phase that bar chart into the screen area..
Thanks
Jack
 
Ivan Jozsef Balazs
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd better check whether the JFreeChart stuff is designed to be somehow extended/customized, and if yes, how.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!