Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explane me why is it so

 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:
11. static classA {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println(�B �); }
16. }
17. public static void main(String[] args) {
18.A a=new B();
19. a.process();
20.}
What is the result?
A. B
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
F. Compilation fails because of an error in line 19.
Answer: F
 
Gaurav Bhatia
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The call is using reference of class A. And as per the method definition i.e process method in class A, it throws an Exception. So, while making the call you need to enclose this in try catch blocks.
HTH
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to QuoteYourSources when posting questions like this.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was this the whole source given? This code works only if present in a well formed class.

It had me confused a bit.
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we mark a class static like this. I read that only inner nested class could be marked static.
 
Anit Nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At compile time there is no way for compiler to know what object the reference variable points to, in this case it may be object of A or object of B. Since you are using reference of class A and the method defined for class a throws exception, compiler will generate error if the exception is not handled or declared.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surely the answer is compilation problem

since the code starts from line 11 and hence we can assume that this is part of a valid class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic