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???
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
Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
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.
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.
Raghu<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3
Raghuveer Kumarakrishnan
Ranch Hand
Joined: Mar 13, 2005
Posts: 32
posted
0
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
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.
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
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.