Is it possible to override toString method in an array using Java?


You can override the toString() method of the Object class but, if you are creating an object array of a particular class and want to print the contents of this array by overriding the toString() method instead of the, you cannot do that there is no solution for that in Java as of now.

But you can achieve this using various other methods −

Using the toString() method of the Arrays class

The toString() method of the Arrays class accepts a String array (in fact any array) and returns it as a String. Pass your String array to this method as a parameter. You can simply pass your array of objects to this method.

Example

 Live Demo

import java.util.Arrays;
class Student {
   String name = "Krishna";
   int age = 20;
   Student(String name, int age) {
      this.name = name;
      this.age = age;
   }
   public String toString() {
      return "Name: "+this.name+" "+"Age: "+this.age;
   }
}
public class Example {
   public static void main(String args[]) {
      Student std1 = new Student("Krishna", 20);
      Student std2 = new Student("Radha", 25);
      Student std3 = new Student("Trupthi", 30);
      Student std4 = new Student("David", 35);
      Student std5 = new Student("Moksha", 40);
      Student students[] = {std1, std2, std3, std4, std5};
      System.out.println(Arrays.toString(students));
   }
}

Output

[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]

Using the asList() method of the Arrays class

This method accepts an array as an argument and, returns a List object. Use this method to convert an array to Set.

Example

 Live Demo

import java.util.Arrays;
class Student {
   String name = "Krishna";
   int age = 20;
   Student(String name, int age) {
      this.name = name;
      this.age = age;
   }
   public String toString() {
      return "Name: "+this.name+" "+"Age: "+this.age;
   }
}
public class Example {
   public static void main(String args[]) {
      Student std1 = new Student("Krishna", 20);
      Student std2 = new Student("Radha", 25);
      Student std3 = new Student("Trupthi", 30);
      Student std4 = new Student("David", 35);
      Student std5 = new Student("Moksha", 40);
      Student students[] = {std1, std2, std3, std4, std5};
      System.out.println(Arrays.asList(students));
   }
}

Output

[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]

Using the ArrayList class

This is a bit different but solution −

  • Create a class extend the ArrayList class.

  • Add the objects to this class.

  • Use the toString() method of the ArrayList class to print the contents.

Example

 Live Demo

import java.util.ArrayList;
import java.util.Arrays;
class Student {
   String name = "Krishna";
   int age = 20;
   Student(String name, int age) {
      this.name = name;
      this.age = age;
   }
   public String toString() {
      return "Name: "+this.name+" "+"Age: "+this.age;
   }
}
public class Example extends ArrayList<Object> {
   public static void main(String args[]) {
      Example obj = new Example();
      obj.add(new Student("Krishna", 20));
      obj.add(new Student("Radha", 25));
      obj.add(new Student("Trupthi", 30));
      obj.add(new Student("David", 35));
      obj.add(new Student("Moksha", 40));
      System.out.println(obj.toString());
   }
}

Output

[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]

Updated on: 14-Oct-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements