• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quetion on inheritance

 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic