Write a Java program to Method Overriding with Exception Handling


In this Java program, you are demonstrating method overriding and the use of exception handling. The program defines a Parent class and a Child class that inherits from Parent. The Child class overrides the display method and changes the exception type it can throw. Here's an explanation of the program:

  • public class MethodOverride: This is the main class that contains the main method, the entry point of the program.
  • public static void main(String[] args): The main method is where the program execution starts. It creates an object of the Child class and attempts to call the display method on it.
  • Parent parent = new Child();: This line creates an object of the Child class but assigns it to a reference of the Parent class. This is allowed because Java supports polymorphism, where a reference variable of a parent class can refer to an object of a child class.
  • try { parent.display(); } catch (Exception e) { e.printStackTrace(); }: This code attempts to call the display method on the parent object, which is actually an instance of the Child class. It is surrounded by a try-catch block to handle any exceptions that may be thrown during the execution of the method.
  • class Parent: This is the parent class, which has a method named display.
  • void display() throws Exception: The display method of the Parent class is declared to throw an Exception. It prints "Parent's display method" to the console.
  • class Child extends Parent: This is the child class that inherits from the Parent class.
  • @Override void display() throws RuntimeException: The Child class overrides the display method and changes the exception type it can throw. It throws a RuntimeException instead of an Exception. It prints "Child's display method" to the console.

In this program, the display method in the Child class overrides the one in the Parent class, and it changes the type of exception it can throw. This is allowed because a subclass method can throw a subtype of the exception thrown by the parent method.

When you call the display method on the parent object, it actually invokes the display method of the Child class, and "Child's display method" is printed to the console. However, the display method in the Child class throws a RuntimeException, which is not explicitly caught in the main method. Therefore, an unhandled exception occurs, and the stack trace is printed to the console.

The throws clause in the method declaration indicates the types of exceptions that a method can throw. Subclasses can throw exceptions that are subclasses of the exceptions thrown by the parent class method, but they cannot throw broader exceptions.


Source Code

public class MethodOverride	 // Main class
{
	public static void main(String[] args)
	{
		Parent parent = new Child();
		try
		{
			parent.display();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
 
class Parent  // Parent class	
{
	void display() throws Exception
	{
		System.out.println("Parent's display method");
	}
}
 
class Child extends Parent	// Child class
{
	@Override
	void display() throws RuntimeException
	{
		System.out.println("Child's display method");
	}
}

Output

Child's display method