Overriding toString() Method in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 67 Likes Like Report Java being object-oriented only deals with classes and objects so do if we do require any computation we use the help of object/s corresponding to the class. It is the most frequent method of Java been used to get a string representation of an object. Now you must be wondering that till now they were not using the same but getting string representation or in short output on the console while using System.out.print. It is because this method was getting automatically called when the print statement is written. So this method is overridden in order to return the values of the object which is showcased below via examples. Example 1: Java // file name: Main.java class Complex { private double re, im; public Complex(double re, double im) { this.re = re; this.im = im; } } // Driver class to test the Complex class public class Main { public static void main(String[] args) { Complex c1 = new Complex(10, 15); System.out.println(c1); } } OutputComplex@214c265e Output Explanation: The output is the class name, then 'at' sign, and at the end hashCode of the object. All classes in Java inherit from the Object class, directly or indirectly (See point 1 of this). The Object class has some basic methods like clone(), toString(), equals(),.. etc. The default toString() method in Object prints "class name @ hash code". We can override the toString() method in our class to print proper output. For example, in the following code toString() is overridden to print the "Real + i Imag" form. Example 2: Java // Java Program to illustrate Overriding // toString() Method // Class 1 public class GFG { // Main driver method public static void main(String[] args) { // Creating object of class1 // inside main() method Complex c1 = new Complex(10, 15); // Printing the complex number System.out.println(c1); } } // Class 2 // Helper class class Complex { // Attributes of a complex number private double re, im; // Constructor to initialize a complex number // Default // public Complex() { // this.re = 0; // this.im = 0; // } // Constructor 2: Parameterized public Complex(double re, double im) { // This keyword refers to // current complex number this.re = re; this.im = im; } // Getters public double getReal() { return this.re; } public double getImaginary() { return this.im ; } // Setters public void setReal(double re) { this.re = re; } public void setImaginary(double im) { this.im = im; } // Overriding toString() method of String class @Override public String toString() { return this.re + " + " + this.im + "i"; } } Output10.0 + 15.0i Create Quiz Comment K kartik Follow 67 Improve K kartik Follow 67 Improve Article Tags : Java java-overriding Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like