
모든 클래스는 Object 클래스를 상속받고 있음의 표시가 생략되어 있다.
따라서, 모든 클래스는 toString() 메서드를 사용할 수 있다.
toString() 객체가 가지고 있는 정보나 값들을 문자열로 만들어 리턴한다.
toString()을 @Override 재정의하여 사용할 수 있다.
@Override
public String toString() {
return "Hi this coffee is " + this.getPrice();
...
...
public class HelloWorld {
public static void main(String[] args) {
Coffee latte = CoffeeFactory.getCoffee("Latte", 4000);
System.out.println(latte);
/*
Hi this coffee is 4000
*/