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 Exploring the Java Collection Framework Sets

Benjamin McKenzie
Benjamin McKenzie
7,455 Points

Java Data Structures Set Challenge

I understand how to pass this challenge, but I have a question. In the first line of the Blog class it reads, " List<BlogPost> mPosts; " I thought you weren't allowed to instantiate a List<E> because it is just an interface and not a class. All other code I've seen has been of the form, " List<E> exampleName = new someClass<E>(); " where someClass is just a class that implements the list interface. Why does the code in the challenge not cause an error?

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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class BlogPost implements Comparable<BlogPost>, Serializable {
  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 int compareTo(BlogPost other) {
    if (equals(other)) {
      return 0;
    }
    return mCreationDate.compareTo(other.mCreationDate);
  }

  public String[] getWords() {
    return mBody.split("\\s+");
  }

  public List<String> getExternalLinks() {
    List<String> links = new ArrayList<String>();
    for (String word : getWords()) {
      if (word.startsWith("http")) {
        links.add(word);
      }
    }
    return links;
  }

  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;
  }
}
com/example/Blog.java
package com.example;

import java.util.List;

public class Blog {
  List<BlogPost> mPosts;

  public Blog(List<BlogPost> posts) {
    mPosts = posts;
  }

  public List<BlogPost> getPosts() {
    return mPosts;
  }
}

1 Answer

Daniel Babbev
Daniel Babbev
10,354 Points

This is called polymorphism and it is way to make different implementations on same methods in the same object. This may sound confusing at fist, so I'll explain it with an example.

First we create an interface

interface Animal{
    void doSound();
}

Then I can create as much classes, as I like to implement this interface. In different classes the method doSound() will have different implementations /meaning, functions/

public class Dog implements Animal{

      //The method is overridden and it's implementation is different  
      @Override
      public void doSound(){
          System.out.println("Bark, bark!")
      }
}
public class Goat implements Animal{

      //The method is overridden and it's implementation is different  
      @Override
      public void doSound(){
          System.out.println("Bleat!")
      }
}
public class Cat implements Animal{

      //The method is overridden and it's implementation is different  
      @Override
      public void doSound(){
          System.out.println("Meow!")
      }
}

Now the different implementations of Animal will produce different outputs!

Animal doge = new Dog();
Animal garfield = new Cat();
Animal myGoat = new Goat();

doge.doSound();
//Output: Bark!

garfield.doSound();
//Output: Meow!

myGoat.doSound();
//Output: Bleat!