File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception code " Watch "Exception code " New topic

Exception code

agilemanoj kumar
Ranch Hand

Joined: Mar 07, 2008
Posts: 70

Why compile time error ?
How to rectify this problem ?

Manoj Kumar
krishnamoorthy kitcha
Ranch Hand

Joined: Feb 04, 2006
Posts: 96
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.

Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
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

Joined: Oct 27, 2007
Posts: 177
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();

Additional :

Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
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
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42956

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

Joined: Feb 25, 2008
Posts: 61
Hi Ulf,
thanks for note above. will be more careful next time.

agilemanoj kumar
Ranch Hand

Joined: Mar 07, 2008
Posts: 70
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:
subject: Exception code
It's not a secret anymore!