014-008-010

Overriding toString()

Easy

Problem Description

Overriding toString()

In this problem: You will override the toString() method from the Object class in a Product class to return a string representation of product information.

Learning Objective: Understand how to override the toString() method inherited from the Object class

Overview

All Java classes implicitly inherit from the Object class. By overriding toString(), you can display meaningful information when an object is printed with System.out.println().

Specifications

  • Product class: name (String), price (int) fields
  • Override toString() to return format: "Product[name=productName, price=priceValue]"
  • Create a Product instance in Main and print it directly with println()

Output Format

Product[name=Laptop, price=89000]

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up