user288245
user288245

Reputation: 23

Describing Types question

I have a bunch of types (eg. LargePlane, SmallPlane) that could be in this collection i've made, how do i print like LargePlane? I've tried like typeOf() and stuff but it doesn't work. Within like a toString()? So when i output the collection it states what type it is.

Upvotes: 0

Views: 81

Answers (2)

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102398

Use .getClass().getName()

The following example uses a Class object to print the class name of an object:

void printClassName(Object obj)
{
     System.out.println("The class of " + obj +
                       " is " + obj.getClass().getName());
}

For more details, take a look at JavaDoc for Class.

Upvotes: 4

road242
road242

Reputation: 2532

You could either implement an interface or extend something like a "Plane" class

public class LargePlane implents IPlane {

  public String toString() {
    // build return string here
  }

}


public interface IPlane {
  public String toString();
}

Upvotes: 0

Related Questions