Java Java Data Structures Getting There Type Casting

Razvan Cirstea
Razvan Cirstea
2,493 Points

How do I solve the second task for type casting?

Hi,

I don't understand why using this code:

public static String getTitleFromObject(Object obj) {

String result = ""; if ( obj instanceof String){ result = (String) obj;

} else if (obj instanceof BlogPost){

 BlogPost blog = (BlogPost) obj ;
 result = blog.getTitle();

} return result; }

Checking my code, the compiler lets me know that Task 1 is no longer passing (return obj type casted as tring if it is in fact a String) .

Could somebody please explain me how to solve the second task, please ?

3 Answers

andren
andren
28,468 Points

Do you have one of the hints enabled? If you try to solve the challenge with one set to true it won't let you pass the challenge. If that is not the case then I don't what could be wrong as the code you posted is completely fine.

When I tested your code with the hints disabled like this:

import com.example.BlogPost;

public class TypeCastChecker {
  /***************
  I have provided 2 hints for this challenge.
  Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
  NOTE: You must set all the hints to false to complete the exercise.
  ****************/
  public static boolean HINT_1_ENABLED = false;
  public static boolean HINT_2_ENABLED = false;

  public static String getTitleFromObject(Object obj) {
    String result = "";
    if ( obj instanceof String){
      result = (String) obj;
    } 
    else if (obj instanceof BlogPost){
      BlogPost blog = (BlogPost) obj ;
      result = blog.getTitle();
    } 
    return result; 
  }
}

The code was accepted and I could complete the challenge without any issues. The challenges can be a bit buggy at times so maybe try restarting it and paste in the code above, that should work.

Razvan Cirstea
Razvan Cirstea
2,493 Points

Hi Andren, you were right ! I had a hint enabled as "true" :) Many thanks for opening my eyes :D

Sandro Müller
Sandro Müller
1,089 Points

I quite do not get it. If the object is of type String why do we have to downcast the object? I mean result is also from type String. Since both are from type String, why do we need a casting? (Question from 04.12.17)


1.

  1. ------ (Answer from myself 05.12.17)

We pass an object to a method. This object can be of any kind. So we check, if the object is actually of type <String> by using the <instanceof> keyword. If (obj instanceof String) is <True>, then we assign <result> to the <obj>. But here is the problem. <String result> is of type <String> and <obj> is of type <Object>. This assignment is not possible, since a <String> is in its hierarchy lower than <obj> of type <Object>, even if it contains a <String>. So we downcast it: <result = (String) obj>.

  1. ------ Important: We can only downcast it to a <String>, since <obj> actually contains a <String>. This should answer my own first question. Well done Sandro xD

(Question from 04.12.2017) Even less I understand the 2nd part. We are asking, if the object that was passed into the method is from type BlogPost. Isn't BlogPost a class?(1)

And these lines I have no idea what they are doing: (2)

  1. ------ BlogPost blog = (BlogPost) obj ;
  2. ------ result = blog.getTitle();

(Answer from myself 05.12.2017) Let me try to answer this question by myself again.

(1) Think about it. Every object created in the class <BlogPost> is from type <BlogPost>. So, of course we ask if it is from type <BlogPost>, since the object might have come from the class < BlogPost>. I guess that makes sense.

(2) Let's say our method get an object of type <BlogPost>. Can we cast it then to <BlogPost>? Well, only if the object that our method got, actually contains something of type <BlogPost>. So before doing this cast, we are going to ask:

if (obj instanceof BlogPost) is <True> then assign it. But this time we are not supposed to assign it just to <result>. <result> should return <getTitle()>, which is a method of the class <BlogPost>. Well, we know that our object is of type <BlogPost>. So we can actually downcast it without a problem and create a new object reference to that object. BlogPost blog = (BlogPost) obj.

After the downcast, <obj> has access to all methods of <BlogPost> - so does <blog>. Now, we can write <result = blog.getTitle> and return its value and finish the challenge. Instead of creating a new reference we can actually directly return the value as following: <return result = (BlogPost) obj.getTitle())>