• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting methods  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 74
  • Mark post as helpful
  • send pies
  • 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: 56610
172
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This tutorial page explains casting: Inheritance - The Java Tutorials.
 
Nico Van Brandt
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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
  • 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: 74
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!