Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot figure out the result

 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

Herein I'm writing 2 programs.
Prog1:
////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////

The result is compiler error stating
./scjp/Exception/Math.java:9: exception scjp.Exception.Math.MyException is never
thrown in body of corresponding try statement
catch(MyException e){P.rintln("1");}

/////////////////////////////////////////////////////////////////////

while Prog 2:



//////////////////////////////////////////////////////////////////

It compiles fine and result is 3

Plz explain why the disparity in answers???

Regards ,
Akshay

EDIT by mw: Added Code Tags.
[ December 10, 2005: Message edited by: marc weber ]
 
gutta rahul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first sample the reference "m" given is of type MathObj, which is resolved at compile time, which is static binding. In the second sample the reference "m" is of super type Math and is resolved at runtime, which is late binding.
 
tim bond
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Math{ public class MyException extends Exception{}
public static void main(String[] args){
MathObj m=new MathObj();
try{ m.op();}
catch(MyException e){ System.out.println("1");} }
public void op()throws MyException{
System.out.println("2");throw new MyException();}}
class MathObj extends Math{
public void op(){ System.out.println("3");}}

in the first m is object of Mathobj and reference of MathObj. to resolve the error
you need to remove the System.out.println("2");throw new MyException();
to System.out.println("2"); as the exception is already in try and catch block.
there is no need to throw it again. This is causing the error.

public class Math{
public class MyException extends Exception{}
public static void main(String[] args){
Math m=new MathObj(); try{ m.op();}
catch(MyException e){ System.out.println("1");} }
public void op()throws MyException{
System.out.println("2");throw new MyException();}}
class MathObj extends Math{
public void op(){ System.out.println("3");}}


here the m is object of Math but reference of MathObj. so it just executes the m.op in class mathobj .
 
tim bond
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you have to modify the public void op()throws MyException{
System.out.println("2");throw new MyException();}}
to
public void op(){
System.out.println("2");throw new MyException();}}
this will then print 2 and 1 ;
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Ranchers,

I'm not statisfied with the response.Plz elaborate further.
As far as Tim response is concerned I guess ,he mess up the question.
My doubt is basically on this line

Math m=new MathObj();
&
MathObj m=new MathObj();

In first line the method op() is called of class MathObj because of polymorphism and late binding,but also in 2nd line op() is called of MathObj.Isn't it???
Then why the disparity???
What does compilation error has to do with static binding or late binding

Regards,
Akshay
 
Thomas Drew
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akshay , you are overriding the op option in MathOpt. The Op in MathOpt does not throw an exception. Where as the option in Math does. To fix the problem you will have to either throw the exception in the MathOpt class, remove the statement from the try block, or call the op option in the super class. If you elect to have the class throw an exception you will have to change the class Mathopt to throw an exception as well.
Hope this helps.
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

Regarding my problem and Thomas's response,my doubt isn't yet cleared.
Specifically , my doubt is why the disparity on the output by just changing the reference of m (first MathObj and second Math)since both the time MathObj's op is being called.
Regards ,
Akshay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic