Java Java Data Structures Getting There Type Casting

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

typecasting challenge part 2

I don't understand what it means to cast it to a Blogpost. I don't completely understand typecasting.

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost {
    private String mAuthor;
    private String mTitle;
    private String mBody;
    private String mCategory;
    private Date mCreationDate;

    public BlogPost(String author, String title, String body, String category, Date creationDate) {
      mAuthor = author;
      mTitle = title;
      mBody = body;
      mCategory = category;
      mCreationDate = creationDate;
    }

    public String getAuthor() {
      return mAuthor;
    }

    public String getTitle() {
      return mTitle;
    }

    public String getBody() {
      return mBody;
    }

    public String getCategory() {
      return mCategory;
    }

    public Date getCreationDate() {
      return mCreationDate;
    }
}
TypeCastChecker.java
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) {
    // Fix this result variable to be the correct string.
    String result = "Is a string";
    Boolean isString=obj instanceof String;
    if(isString){
        result=(String) obj;
    }
    return result;
  }
}

1 Answer

Hi, Im still newibe in java so sry if i confuse u even more XD. the working code is:

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) {
    // Fix this result variable to be the correct string.
    String result = "";
    if(obj instanceof String)///first we check if obj is oftype String.
    {
//if it is we cast the obj parameter as a String, when we do that we now have access to the String  methods and properies.//
      return (String) obj;
    }
    else if(obj instanceof BlogPost)//now we check if obj is of type BlogPost
    {
//if it is we cast obj to BlogPost and name our "access point" blogpost in order again to gain access to BlogPost's 
 properties and methods in order to use the getTitle() method that BlogPost class have.//
      BlogPost blogpost = (BlogPost) obj;

//after we cast obj to BlogPost we store the title of the blogpost in result
      result = blogpost.getTitle();

    }
    return result;//then we return result 


  }
}