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:
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."
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"); } }
Parent class display method Child 1 class display method Parent class display method Child 2 class display method
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions