0
class Sup{
    public static String get(Integer i){
        return "Sup";
    }
}

class Sub extends Sup{
    public static String get(Integer i) throws IOException{
        return "Sub";
    }
}

Above program gives compile error Exception IOException is not compatible with throws clause in Sup.get(Integer)

Both the methods are static, so it's not a case of overriding. Then why this error?

Just to make it clear that it's not a case of overriding, if I add Override annotation to subclass:

@Override
    public static String get(Integer i){
        return "Sub";
    }

It gives compile error The method get(Integer) of type Sub must override or implement a supertype method.

What am I missing with Exception in subclass method that is static?

Pankaj
  • 5,132
  • 3
  • 28
  • 37
  • 2
    **Both the methods are static, so it's not a case of overriding.** wrong. – Mohammed Aouf Zouag Feb 06 '16 at 16:53
  • Okay, got that I am trying but since methods are static, actually we are just hiding superclass method. We can never override static method, so why to not allow throwing Exception in subclass method. – Pankaj Feb 06 '16 at 16:58
  • 2
    See the linked duplicate (at the top of your question). – Tunaki Feb 06 '16 at 16:59
  • You are not overriding but you are hiding the method from the subclass... it follows or at least it should follow same rules as override thus you can't add the throw exception in the declaration. – Jkike Feb 06 '16 at 17:01
  • 1
    Thanks @Tunaki, this gives me some explanation. It's part of language and it is what it is. – Pankaj Feb 06 '16 at 17:03

0 Answers0