Code: Select all
public Set getSet(int a) {...}
It seemed correct to me. A method that overrides the getSet method can declare any RuntimeException even if it does not have to.It can declare any RuntimeException in throws clause.
Moderator: admin
Code: Select all
public Set getSet(int a) {...}
It seemed correct to me. A method that overrides the getSet method can declare any RuntimeException even if it does not have to.It can declare any RuntimeException in throws clause.
No, an overriding method cannot throw any new checked exception.Michailangelo wrote:Why throw only RuntimeException? Can't the overriding method throw any kind of exception since the getSet() doesn't throw anything?
Well, that is a good question. You are right it is not actually overriding the method because there is no implementation. But look at it other way, the subclass is indeed giving a new behavior to this method and that is that the method has to be implemented by another subclass. This behavior is different from the original method (which does not force a subclass to override it) and in that sense it is overriding the original method.javanaut wrote:My question is how is declaring an abstract method with the same signature considered an override since the method is not actually implemented until a concrete class extends the abstract class with the abstract method?
admin wrote:Well, that is a good question. You are right it is not actually overriding the method because there is no implementation. But look at it other way, the subclass is indeed giving a new behavior to this method and that is that the method has to be implemented by another subclass. This behavior is different from the original method (which does not force a subclass to override it) and in that sense it is overriding the original method.javanaut wrote:My question is how is declaring an abstract method with the same signature considered an override since the method is not actually implemented until a concrete class extends the abstract class with the abstract method?
HTH,
Paul.
No, I do not agree that you would have a completely separate method. I am not sure why do you think so.Brian B wrote:The verbiage of the question says:
"...are correct for a method that overrides the following method:"
Isn't the answer "It can be abstract" a little misleading?
Considering, that an abstract method cannot have a method body, if you were to create an abstract class (as you'd be required to do if you declare a method abstract) wouldn't you have a completely separate method?
Not sure what you mean. Can you please provide some code to illustrate your point? Return type of a method has nothing to do with it being abstract or not.Wouldn't declaring it abstract violate the rule ("same return type in case of primitives (a subclass is allowed for classes, this is also known as covariant return types"). given in the explanation for overriding a method?
Sure, but the question is above overriding the original method. You can override it with an abstract method. I agree that it doesn't seem to natural but there is nothing wrong with it technically. Putting it another way, what would you call it if a subclass has the same method as its super class but has made it abstract. I think calling it overriding is ok.Based on your further explanation here:
"...You are right it is not actually overriding the method because there is no implementation. But look at it other way, the subclass is indeed giving a new behavior to this method and that is that the method has to be implemented by another subclass. This behavior is different from the original method (which does not force a subclass to override it) and in that sense it is overriding the original method."
If a subclass extends the abstract class, which you create to make the original method abstract, wouldn't the subclass be overriding the abstract method, not the original method presented in the question?
It is only about checked exceptions?.It may throw only those exceptions that are declared in the throws clause of the superclass's method or exceptions that are subclasses of the declared exceptions.
Code: Select all
class Yes extends OtherClass{
OtherClass oc = new OtherClass();
public void rte() throws RuntimeException{}
}
class OtherClass implements Yeah{
public static void main(String[] args){
System.out.println("from OtherClass");
}
public void rte() {}
}
interface Yeah{
public abstract void rte();
}
Users browsing this forum: No registered users and 100 guests