• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception code

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why compile time error ?
How to rectify this problem ?
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :
https://coderanch.com/t/268027/java-programmer-SCJP/certification/pulbic-static-void-main-String

Thanks
Vishal
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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-
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic