Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Question about casting

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm currently going through some training in Java. Most if has gone smoothly, but I'm stuck on one point: casting.

What I've done is created a Java project in eclipse (for learning purposes).

Two basic classes in it: buildings and houses. Houses inherits from buildings:







Nothing to complex aside from a few getter's and setter's.

I also have a tester class:




The part I'm hung up on is the casting. The way it is now, I get: Type mismatch: cannot convert from buildings to int.

I've also tried:



That gives: getStories cannot be resolved or is not a field.

Any ideas what I'm doing wrong?

Any help would be appreciated.


 
Marshal
Posts: 27372
88
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
I don't understand why you're doing any casts at all in that line. The getStories() method returns an int, you want to assign that to an int variable... just do that. Why do you think casting is required there?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brent Pathakis wrote:The way it is now, I get: Type mismatch: cannot convert from buildings to int.



In addition to what Paul said, one has to wonder why you're trying to cast from buildings to int. How do you think that would work and what would it accomplish for you?
 
Brent Pathakis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Thanks for having a look.

You are right about casting not being needed here.

It came up in training, so I wanted to come up with an example (albeit a contrived, and probably invalid one), so I could play with it and get my head wrapped around the concept.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brent Pathakis wrote:
It came up in training, so I wanted to come up with an example (albeit a contrived, and probably invalid one), so I could play with it and get my head wrapped around the concept.



In that case, a good place to start (or to go after you tried it yourself to check your own work) would be to google for java casting examples. Ignore any results that take you to roseindia though. That site is full of misinformation.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brent Pathakis wrote:Paul,

You are probably right about casting not being needed here.



edit the word probably please.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic