Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quetion on inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Quetion on inheritance" Watch "Quetion on inheritance" New topic
Author

Quetion on inheritance

ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Please guide the meaning of this ,correct me if i am wrong:

according to JLS:

�If all the inherited methods are abstract, then the class is necessarily an abstract class and is considered to inherit all the abstract methods. A compile-time error occurs if, for any two such inherited methods, either they have different return types or one has a return type and the other is void. (The throws clauses do not cause errors in this case.)


interface i {
void go();
}
//an absract class
abstract class abs{
abstract void go() throws Exception;
}

class imp extends abs implements i {
public void go () throws Exception {}
}

It causes compile time error as cannot implement go() in i;

But according to JLS ,The throws clauses do not cause errors in this case.)

But let me know where i am wrong
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
you "imp" class is implementing interface "i", go() method signature in "i" doesn't declare exceptions. Hence, your go() method implementation in "imp" class cannot declare exceptions either.
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
Posts: 49
Let me get this straight - we're implementing an interface, which is abstract, and has an implicitly abstract go() method, then we are extending abs, our superclass, which also has an abstract go() method. One throws Exception and the other doesn't, but how do we know which go() we mean to make concrete in class imp?

When I remove the throws clause from the concrete go() method in class imp, which should be capitalized Imp according to naming conventions, the code compiles fine. It seems that implementation takes precedence.


EXCEL IN ALL YOU DO
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Maybe someone else can better clarify but I don't think it's an issue of one taking precedence. It seems as if either the interface or the abstract class is leaving off the throws Exception, and you then try to create your go method with a throws Exception you'll run into a compile error. This seems to make sense to me since you can't throw Exceptions that your super class or interface do not throw and it would make sense that you'd have to fufill the obligation of both the interface and abstract class go() declarations. Apparently it's not enough that "just one of your interfaces or abstract classes throws the Exception."
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
In such a case, the overriding method is said to override both the inherited abstract methods.

The rules of overriding w.r.t throwing exceptions are being violated here.

The interface method does not declare that it throws any checked exception but the overriding method declares that it throws Exception. The overriding method is free to not have a throws clause even for a checked exception. Hence, no issues with the abstract class method.
[ October 16, 2006: Message edited by: Aniket Patil ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quetion on inheritance
 
Similar Threads
Overriding Static Method Question
Static Method and Inheritence.
Classes: Abstract And Interface
Marcus Green - Qn : 23 - Doubt
my notes on JLS for any1 who needs them !!