File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception code " Watch "Exception code " New topic
Author

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.

Regards
k.krishnamoorthy.
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).


SCJP 5
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();
a.process();
}
}



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

Thanks
Vishal
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
-mila-
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41063
    
  43
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.


Ping & DNS - my free Android networking tools app
Vierda Mila
Ranch Hand

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

regards
-mila-
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: http://aspose.com/file-tools
 
subject: Exception code
 
Similar Threads
Regarding static classes
can class be static?
why does this compile?
About Static