This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Question about casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about casting" Watch "Question about casting" New topic
Author

Question about casting

Brent Pathakis
Greenhorn

Joined: Jul 20, 2011
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 20, 2011
Posts: 4
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 21, 2012
Posts: 216

Brent Pathakis wrote:Paul,

You are probably right about casting not being needed here.



edit the word probably please.


OCPJP 6 86%
 
 
subject: Question about casting