Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

Q-4 of Diagnostic

Dear Fred,


"Note that thrown exceptions are caught by catch blocks based on the runtime type of the thrown exception, instead of its reference type. This is in contrast to how overloaded methods are invoked."


Can you please give an example of how it works in overloaded methods.


Thanks,

Pritha

Based on the JavaSE classes, think about something like this:


public void print(Integer x) {
  System.out.println("Integer");
}

public void print(Number x) {
  System.out.println("Number");
}


We know Integer is a sub-class of Number. So we can create an object like this:


Number obj = new Integer(0);


Then if we pass this object to the overloaded print method, what do you think the output will be?


print(obj);


Would that print "Number" because that was the reference type, or would it print "Integer" because that was the object type?

It would print "Number". I got it.

ExamLab © 2008 - 2024