Seshachala Dharmaveer Kalluri

Greenhorn
+ Follow
since Dec 16, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Seshachala Dharmaveer Kalluri

Hi Sagarika,
The main point to be noted is that, abstract and native methods should not have a body for them. They are supposed to be defined elsewhere.
Abstract methods will be defined in subclasses, where as native methods will be defined entirely outside the JVM. That is why a method cannot be both native and abstract.
so, the class implementation in the given example,
class G implements Z {public native void m1();} is perfectly correct. If u remove that ; and replece it with {}, it will give a compiler error saying Native methods can't have a body. Hope this removes ur confusion.
Dharmaveer

Originally posted by Purvesh Vora:
Hi,
There is one exception question from Dan Chisholm.
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (WhiteException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
What is the result of attempting to compile and run the program?
a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compile-time error
g. Run-time error
h. None of the above
The answer is f.
Can anybody plese explain the same.
Thanks & Regards,
Purvesh Vora
------------------
IBM Test 486(OOAD With UML)


Yes the correct answer is f, because in the first try block of the main method, the method call White.m1() is throwing an exception of class ColorException, which is being caught by an object of its subclass WhiteException, which cannot be done.