• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting methods

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

i have a question. Let's say we have


What does the code on line 2. Is this some kind of converting from HttpSession to String. When we can use this method for converting?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not valid code. Please show us the actual code for which you have a question.

In any case, what I think you are asking about is called "casting".
 
Krasimir Ivanov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer. That's what i meant "casting". The source code is not important. I want to know in which cases this converting method is applicable?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd use that when the method is declared to return an Object, but it actually returns a String (a String IS-AN Object, so this is valid), and you want to assign it to a String variable. However, you have to be certain it will return a String, otherwise you'll get an Exception.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or when you want to have a list of dogs (where all types of dogs extend from Dog)

When you then get the dogs from the ArrayList, you want to cast it from a Dog to a Poodle, Labrador, etc...
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to avoid casting like that, because it is error-prone. You are better using polymorphism, so you declare your methods in the Dog class, and a Poodle will have the same methods.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This tutorial page explains casting: Inheritance - The Java Tutorials.
 
Nico Van Brandt
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Off course they will have the same methods, but the bark of a Labrador will not sound the same as the bark of a Chihuahua.
For that, in my example given, you have to cast the Dog objects.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nico Van Belle wrote:Off course they will have the same methods, but the bark of a Labrador will not sound the same as the bark of a Chihuahua.
For that, in my example given, you have to cast the Dog objects.


No you don't - that's exactly what polymorphism is for.

You only need to cast if you have different methods in the subclasses. For instance, your Dog class might have a run() method, but your Greyhound class might also have a runReallyReallyFast() method. You'll need to cast to get at the extra methods.
 
Nico Van Brandt
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha yes, you are correct. The example is not a good one.
Someway I thought you had to cast it. My mistake
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic