This Java program demonstrates the use of the super keyword to access a member of the parent class from a subclass. In this program, there are two classes: Parent (the parent class) and Child (the child class). The Child class extends the Parent class and accesses a protected member from the parent class using the super keyword. Here's an explanation of the program:
This program demonstrates how to use the super keyword to access a member of the parent class from the child class. In this case, the Child class accesses the protected msg member from the Parent class and prints its value to the console. The super keyword is used to explicitly refer to the parent class's member, allowing you to access and use it in the child class.
public class SuperClassMember // Main class { public static void main(String[] args) { Child child = new Child(); child.displayMessage(); } } class Parent // Parent class { protected String msg = "Hello World"; } class Child extends Parent // Child class { void displayMessage() { System.out.println(super.msg); // Accessing parent class member using super keyword } }
Hello World
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions