Open In App
Related Articles

Java | Inheritance | Question 9

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
final class Complex {
  
    private final double re;
    private final double im;
  
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
  
    public String toString() {
        return "(" + re + " + " + im + "i)";
    }
}
  
class Main {
  public static void main(String args[])
  {
       Complex c = new Complex(10, 15);
       System.out.println("Complex number is " + c);
  }         
}

                    
(A)
Complex number is (10.0 + 15.0i)
(B) Compiler Error (C)
Complex number is SOME_GARBAGE
(D)
Complex number is Complex@8e2fb5
Here 8e2fb5 is hash code of c

Answer: (A)

Explanation: SeeĀ https://www.geeksforgeeks.org/overriding-tostring-method-in-java/

Quiz of this Question

Last Updated : 28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads