Kunal Java Unit-2
Kunal Java Unit-2
subpackage.
// Base class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Derived class
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Method of Dog class
}
}
Advantages:
Reuse code: You don't have to start from scratch. You can use parts of the original
blueprint (like the walls and roof) for all your houses.
Easier to change: If you decide to improve the roof design, you only need to
change it once in the original blueprint. All your houses will have the better roof.
Add new things: You can add extra rooms or a pool to a specific house without
changing the basic blueprint.
Organize things: You can group similar houses together (like all the big houses)
to make it easier to find what you need.
Hide details: You can keep the details of how the walls are built hidden, so you
only need to worry about the overall design of the house.
2. What is ‘super’ keyword in java? What are its purposes?
ANS- The super keyword in Java is used to refer to the immediate parent class of the
current object. It serves 2 main purposes in inheritance, calling superclass
constructors and accessing superclass methods and variables.
Calling Superclass Constructor
class Animal {
Animal() {
System.out.println("Animal constructor called");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the constructor of Animal
System.out.println("Dog constructor called");
}
}
• Checked exceptions are exceptions that the compiler forces you to either handle
using a try-catch block
• They usually occur due to programming bugs or logical errors and can be
avoided with proper coding practices.
Examples include: NullPointerException, ArrayIndexOutOfBoundsException,
ArithmeticException.
public class UncheckedExceptionExample {
public static void main(String[] args) {
int num = 10;
int result = num / 0; // ArithmeticException occurs here
System.out.println(result);
}
}
6. Explain exception handling in java using “try”, “catch” and “finally” block.
ANS- Exception handling in Java allows you to deal with unexpected or exceptional
situations that may occur during the execution of a program.
• try block: Contains the code where an exception might occur.
• catch block: Catches the exception thrown in the try block.
• finally block: Optional block it's used to perform cleanup actions (closing files,
releasing resources, etc.).
import java.io.*;
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
// Code where an exception might occur
int result = 10 / 0; // ArithmeticException: Division by zero
} catch (ArithmeticException e) {
// Catch block handling the exception
System.out.println("ArithmeticException caught: " + e.getMessage());
} finally {
// Finally block for cleanup or final operations
System.out.println("Finally block executed");
}
}
}
7. Differentiate bw throw and throws.
ANS-
Mechanism Throws an exception object Declares that the method may throw
explicitly. certain exceptions.
Impact on Stops the normal flow of Doesn't affect the method's actual
Flow execution. execution; used for notification.