File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception code

 
agilemanoj kumar
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why compile time error ?
How to rectify this problem ?
 
krishnamoorthy kitcha
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A { public void process() throws Exception{ System.out.print("A "); }} public class B extends A {public void process() { System.out.println("In B"); } public static void main(String[] args) { A a=new B(); a.process();}}


-----

Hi , the above code is wrong

becasue A is Super class and not is a subclass

It is only applicable B a = new A();
not as A a = new B()
B is low level Hierarchy , A is high level Hierarchy.

Check and tell me your feedback.

Regards
k.krishnamoorthy.
 
Vierda Mila
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi agielmanoj,
your code not compile because at compile time, compiler assume you're calling the supertype version of the method. since that method throw exception, you have to use try-catch block when call that method to avoid compiler error.
and note that above code will print subclass method version at runtime (print "in b).
 
Vishal Chugh
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Manoj,

Nice question ,you must be knowing that binding is done at compile time but invocation is done at runtime , here no doubt overridden version will be called but from compile time it has the info that this method will throw exception , and if main doesnt handle then who else ?

I modified your program a bit in main method as below , its compiling and running


class A {
public void process() throws Exception
{ System.out.print("A "); }
}

public class B extends A {
public void process() {System.out.println("In B"); }

public static void main (String[] args) throws Exception
{
A a=new B();
a.process();
}
}



Additional :
http://www.coderanch.com/t/268027/java-programmer-SCJP/certification/pulbic-static-void-main-String

Thanks
Vishal
 
Vierda Mila
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all ranchers,
when i'm posting above I got connection down, and I try click post reply multiple times and I don't realize it will result that way.
zillion sorry for inconvenience caused.

thanks & regards
-mila-
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vierda,

you can delete a post of yours by clicking the little paper-and-pencil icon, and then on the next page checking the "Delete Post" checkbox, and clicking the "Edit Post" button.
 
Vierda Mila
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,
thanks for note above. will be more careful next time.

regards
-mila-
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vishal for solving this problem.... I could not see that, I am calling a method which can throw an exception, and I am calling that method from main() , so obviously there should be some means, by which main() method should catch this exception. Either throws Exception or try/catch block will work fine for this code...

Hi krishnamoorthy, thanks for showing so much of interest solving this problem. But, it seems your reference basic is not so clear... The block which you posted will never work... If A is super class and B is subclass then you cant refer A's object to B reference... Means, B a = new A() is certainly wrong and will throw compiler error...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic