• 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

Exceptionhandling

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the output when you compile and execute the following program.
Exception is the base class for all the Exception classes.

////////////////////////////////////////////
import java.io.*;

class Base
{
void test() throws IOException {
System.out.println("Base.test()");
String a = null;
//Complex logic goes here
if(a == null)
throw new IOException("");
}

}
////////////////////////////////////////////
public class Child extends Base {

protected void test() throws Exception {
System.out.println("Child.test()");
Base.test(); //Call the parent method
throw new Exception("");
}

static public void main(String[] a) {
try {
new Child().test();
}
catch(Exception e) { }
}
}
Select most appropriate answer.

a) Child.test()
Base.test()
b) Compilation Error: The method void test() declared in class Child cannot override the method of the same signature declared in class Base. Their throws clauses are incompatible.
c) Compilation Error: The method void test() declared in class Child cannot override the method of the same signature declared in class Base.The access modifier is made more restrictive.
d) Runtime error. Cannot make the access modifier more restrictive for test()
<BOLD>
I think the answer should be b) but the given answer was c).
</BOLD>
When i tried to compile the above program i got the compilation error as in b).
Any comments???
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes :-) You and the compiler are both right. Again, do us the favor: WHERE is that question (the answer, really) from?
 
Roll
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this Question from Jargon(Exam simulator). http://www.sarga.com/java/jac.htm
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are 2 reasonxs for getting compile tome error in tis code
1)as mention by u (exception is not handling)
2)The access modifier is made more restrictive.(because overridden method cannot be more private than superclass method)

according to me both answers are right
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I tried the code here on jdk1.5.
The answer b is correct, overridden method shouldn't throw more exception( super clsss) than the one.

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Roll,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention!

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

The access modifier is not made more restrictive. Default access is more restrictive than Protected .The code is fine on that count

Default-- Access Only from same package as parent
Protected --Access Only from same package as parent + Member Inheritance allowed in child no matter what package child is in.

So as discussed above only the Exception overriding is wrong.
 
Raghuveer Kumarakrishnan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roll,

Welcome to Javaranch.

If the compiler says so I would'nt doubt the compiler(as long as you just copy pasted the code)

It is more useful to figure out why that's happening than doubting the compiler

Just my 2 cents
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

class Base is having
void test() throws IOException

class Child is having
protected void test() throws Exception



HERE IS THE RULE : overiding method must have either IOException or its subclass


here Exception is superclass of IOException
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think that even after fix the above test method() in class Child to throw a subclass of Exception the code still doesn't compile just because the call to Base.test() is invalid once test() is not static.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Please note that this thread is over 6 years old, so if you're replying to the original post (or some of the older responses), there's a good chance that those users won't see it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic