This Java program demonstrates the use of the protected access modifier and inheritance. In this program, there are two classes, Person (the parent class) and Student (the child class). The Student class inherits from the Person class and is able to access the protected method defined in the parent class. Here's an explanation of the program:
In this program, the Student class inherits the displayDetails method from the Person class. Despite being marked as protected, it is accessible within the Student class, allowing the showDetails method to call it. When the showDetails method is invoked, it correctly prints "This is a person" to the console, showing that the protected method can be accessed within the derived class
class Student extends Person { void showDetails() { displayDetails(); // Protected method is accessible in the derived class } public static void main(String[] args) { Student student = new Student(); student.showDetails(); } } class Person { protected void displayDetails() { System.out.println("This is a person"); } }
This is a person
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions