Write a Java program to Call the method with the same name using super keyword


This Java program demonstrates the use of the super keyword to call a method from the parent class. In this program, there are three classes: Parent, Child1, and Child2, forming a class hierarchy. The Child1 and Child2 classes inherit from the Parent class. Each of the child classes overrides the display method and calls the display method of the parent class using the super keyword before performing their own specific actions. Here's an explanation of the program:

  • public class SuperKeyword: 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 objects of the Child1 and Child2 classes and calls their display methods.
  • Child1 child1 = new Child1(); and Child2 child2 = new Child2();: These lines create objects of the Child1 and Child2 classes.
  • child1.display(); and child2.display();: These lines call the display method on the child1 and child2 objects, respectively.
  • class Parent: This is the parent class, which contains a method called display.
  • void display(): The display method of the Parent class prints "Parent class display method."
  • class Child1 extends Parent: This is the first child class that inherits from the Parent class. It overrides the display method and calls the super.display() to execute the display method from the parent class and then adds its own message.
  • class Child2 extends Parent: This is the second child class that also inherits from the Parent class. Like Child1, it overrides the display method, calls the super.display() to execute the display method from the parent class, and adds its own message.

This program demonstrates method overriding and the use of the super keyword to call the overridden method of the parent class before adding specific behavior in the child classes. When you call the display method on child1, it first calls the display method in the Parent class and then adds "Child 1 class display method." Similarly, when you call the display method on child2, it calls the display method in the Parent class and adds "Child 2 class display method."


Source Code

public class SuperKeyword	// Main classs
{
	public static void main(String[] args)
	{
		Child1 child1 = new Child1();	// Create objects of the Child classes
		Child2 child2 = new Child2();
 
		child1.display();	// Call the display() method
		child2.display();
	}
}
class Parent	// Parent class
{
	void display()
	{
		System.out.println("Parent class display method");
	}
}
class Child1 extends Parent	 // Child class 1
{
	void display()
	{
		super.display();  // Call the display() method of the parent class
		System.out.println("Child 1 class display method");
	}
}
class Child2 extends Parent	  // Child class 2
{
	void display()
	{
		super.display();  // Call the display() method of the parent class
		System.out.println("Child 2 class display method");
	}
}

Output

Parent class display method
Child 1 class display method
Parent class display method
Child 2 class display method