Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

Can overriding methods change the checked exceptions declared by the parent method? Overriding methods parent method के declared checked exceptions को बदल सकते हैं क्या?

Answer

Java restricts what checked exceptions an overriding method can declare - it can throw the SAME, FEWER, or MORE SPECIFIC (subclass) checked exceptions than the parent, but never new or broader ones.

import java.io.IOException;
import java.io.FileNotFoundException;

class Parent {
    public void readData() throws IOException {
        // ...
    }
}

class ChildOk extends Parent {
    // OK - FileNotFoundException is a SUBCLASS of IOException (more specific)
    @Override
    public void readData() throws FileNotFoundException {
        // ...
    }
}

class ChildAlsoOk extends Parent {
    // OK - declaring NO checked exception at all is always allowed
    @Override
    public void readData() {
        // ...
    }
}

// class ChildBad extends Parent {
//     // COMPILE ERROR - SQLException is NOT a subclass of IOException,
//     // this would be a BROADER contract than the parent allows
//     @Override
//     public void readData() throws java.sql.SQLException {
//     }
// }

// Unchecked exceptions have NO such restriction - a method can
// throw any unchecked (RuntimeException) exception when overriding,
// regardless of what the parent declares
class Parent2 {
    public void process() { }  // declares nothing
}
class Child2 extends Parent2 {
    @Override
    public void process() {
        throw new IllegalStateException('allowed - unchecked exceptions are unrestricted');
    }
}

// Why this rule exists: it preserves Liskov substitution -
// code calling Parent.readData() and only catching IOException
// must still work correctly when a Child instance is used instead

Java यह restrict करता है कि overriding method कौन-सी checked exceptions declare कर सकता है - वो parent जितनी ही, कम, या ज़्यादा specific (subclass) checked exceptions throw कर सकता है, नई या ज़्यादा broad नहीं।

import java.io.IOException;
import java.io.FileNotFoundException;

class Parent {
    public void readData() throws IOException {
    }
}

class ChildOk extends Parent {
    // ठीक है - FileNotFoundException, IOException का subclass है
    @Override
    public void readData() throws FileNotFoundException {
    }
}

class ChildAlsoOk extends Parent {
    // ठीक है - कोई checked exception declare न करना हमेशा allowed
    @Override
    public void readData() {
    }
}

// class ChildBad extends Parent {
//     // COMPILE ERROR - SQLException, IOException का subclass नहीं
//     @Override
//     public void readData() throws java.sql.SQLException {
//     }
// }

// Unchecked exceptions पर यह restriction नहीं है
class Parent2 {
    public void process() { }
}
class Child2 extends Parent2 {
    @Override
    public void process() {
        throw new IllegalStateException('allowed - unchecked पर restriction नहीं');
    }
}

// यह rule Liskov substitution preserve करने के लिए है

Was this answer clear?