Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about casting

 
Brent Pathakis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.


 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brent Pathakis wrote:Paul,

You are probably right about casting not being needed here.



edit the word probably please.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic