Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Getting There Type Casting

How do you use the instanceof for this example?

How to use instanceof for this example

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.

    Object obj = new instanceof Object;
    String result = "";
    return result;
  }
}

1 Answer

Rares Conea
PLUS
Rares Conea
Courses Plus Student 15,000 Points

Hi,

All objects extend Object so for a method that accepts as a parameter an Object you can pass any object you want. For the method in your example you can pass a String because String extends Object so there won t be any errors at compile time but as you can see your method must return a String.The role of the instanceof operator here is to check the type of the object given as parameter when your method is called. For task1 it wants you to check if obj is a String: "if(obj instanceof String)".If obj is a String then you can cast obj to a String and return it: return (String)obj; For task2 it wants you check thay if obj is not a String it may be a BlogPost. If obj is a BlogPost you can cast it to a BlogPost an return the title: else if(obj instaceof BlogPost){ return ((BlogPost)obj).getTitle();} If you don't check if an object is of a specific type and you try to cast it to something wrong, let's say that you want to cast a BlogPost object to a String, then a classcastexception error will be thrown.

This si the code you need for the challenge: public static String getTitleFromObject(Object obj) { if(obj instanceof String){ return (String)obj; } else if(obj instanceof BlogPost){ return ((BlogPost)obj).getTitle(); }

return "";

}