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 - Retired Getting There Type Casting

Javier MARQUEZ
Javier MARQUEZ
11,877 Points

Hello everyone thanks for your help

Hello everyone thanks for your help, I read the doccumentation, I understand how:

  1. Inheritance is useful because it allows us to use properties and methods from the parents.

  2. How by upcasting we can force a parent to use the methods (as soon as they have the same signature) of the child

What I dont get is:

  1. How downcasting is useful at all.
  2. How to pass this code challenge.

I was taking a look at this https://www.youtube.com/watch?v=Uj4JdvFKTNo

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 = true;
  public static boolean HINT_2_ENABLED = true;

  public static String getTitleFromObject(Object obj) {
    String result = "";
    if (obj instanceof String){
      obj = new String;
      result = obj.getType();
    }
    return result;
  }
}

2 Answers

Blake Larson
Blake Larson
13,014 Points

You will see how downcasting is useful in the next challenge. You will downcast an obj to a BlogPost obj to call a method of the BlogPost class. As for the challenge...

public static String getTitleFromObject(Object obj) {
    String result = "";
    if (obj instanceof String) {  // Checking if obj is a String
      result = (String) obj; // if it is.  typecast it as a string so you can save it in the result variable and return result.
    }
    return result;
  }

In the case above, the method takes a parameter of type "Object" (which is the ultimate parent class). So inside the method the parameter "obj" is of type Object. Imagine now you want to do that: if the object is a string, return the length of the string. One cool way is to just type "obj.length". But wait, what if "obj" isn't a string but, say, an integer? An integer doesn't have a length so it wouldn't work. The compiler won't let you type "obj.length" because obj has the type "Object". So it could be any kind of object. The compiler doesn't care that you're paying extra attention to only do that when obj really is a string, that you've checked with "instanceof" and so on. It only cares that obj is of type Object. So only the methods of the superclass Object are available. To use the methods of the subclass, you have to downcast first.