Java Inheritance & Exception Handling Q&A
Java Inheritance & Exception Handling Q&A
A custom exception in Java is defined by creating a class that extends 'Exception' or 'RuntimeException'. For example, class MyException extends Exception. This is advantageous in scenarios where specific application constraints or rules need to be enforced, providing clarity and maintainability by encapsulating error conditions in well-defined exception types .
The 'finally' block is used in exception handling to execute code after a try-catch block and is guaranteed to execute regardless of whether an exception was caught, ensuring cleanup code runs. It will execute except in cases like JVM crashes or System.exit() calls. Its main role is to ensure that resources are freed or other cleanup operations are performed, enhancing robustness .
Method binding in Java refers to the linking of method calls with the method body. Static binding occurs at compile time where method calls and fields are resolved using the reference type (e.g., method overloading). Dynamic binding, or late binding, occurs at runtime where overridden methods are resolved using the actual object's type (e.g., method overriding), supporting polymorphism .
Both abstract classes and interfaces cannot be instantiated and are used to define contract-like structures to achieve abstraction. Abstract classes can have constructors, state, and provide default behavior, supporting partial abstraction. Interfaces, until Java 7, supported full abstraction and facilitated multiple inheritance. While abstract classes suit scenarios where shared state or behavior among subclasses is needed, interfaces are ideal for defining capabilities across unrelated classes .
The 'super' keyword is used in a subclass to call methods and variables of its superclass or to invoke a parent constructor. It is significant in method overriding as it allows the subclass to call the overridden superclass method when needed, thus providing access to perform additional functionality on top of the superclass method .
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime, allowing Java to support runtime polymorphism. It involves a superclass reference pointing to a subclass object, and the appropriate overridden method is invoked at runtime based on the actual object type, not the reference type, enabling dynamic behavior .
Constructor chaining in Java is achieved using the 'super()' keyword, which ensures that the constructor for the parent class is called before the child class constructor. This is crucial in multilevel inheritance where the constructors are executed in a hierarchical order from the superclass down to the subclass, thereby ensuring that all the necessary initialization steps defined in the hierarchy are executed properly .
Interfaces are preferred when full abstraction is needed or when you need to achieve multiple inheritance, which is not possible with abstract classes. This is because a class can implement multiple interfaces but can only extend one abstract class. Interfaces provide a way to define a contract for behaviors without concerning about implementation details, thus promoting loose coupling and high cohesion .
Java does not support multiple inheritance with classes to avoid complexity and ambiguity (e.g., the Diamond Problem). Interfaces however provide a means to achieve multiple inheritance because they allow a class to implement multiple interfaces. This enables a class to inherit method signatures from multiple sources while maintaining clear and conflict-free contract definitions, thus offering flexibility without the downsides of multiple class inheritance .
Checked exceptions must be either declared in the method signature using 'throws' or be handled within a try-catch block, as they are checked at compile time. Examples include IOException. Unchecked exceptions, on the other hand, are not required to be declared or handled explicitly as they occur due to programming errors like NullPointerException; they are checked at runtime .