Java | Inheritance | Question 9
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); } } |
chevron_right
filter_none
(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
Recommended Posts:
- Java | Inheritance | Question 1
- Java | Inheritance | Question 9
- Java | Inheritance | Question 2
- Java | Inheritance | Question 3
- Java | Inheritance | Question 7
- Java | Inheritance | Question 4
- Java | Inheritance | Question 5
- Java | Inheritance | Question 8
- Inheritance in Java
- Interfaces and Inheritance in Java
- Using final with Inheritance in Java
- Delegation vs Inheritance in Java
- Java and Multiple Inheritance
- Inheritance and constructors in Java
- Comparison of Inheritance in C++ and Java



