Write a Java program to demonstrate method overriding with a subclass that overrides a method and changes the access modifier from public to private
The code defines override a method in a subclass while changing the access modifier from public in the superclass to private in the subclass. This code illustrates a fundamental concept in object-oriented programming, specifically in Java, known as method overriding and access control. Here's an explanation of the code step by step:
The key points to understand are:
class Superclass { public void display() { System.out.println("This is a public method in the superclass."); } } class Subclass extends Superclass { // This method attempts to change the access modifier to private // This will result in a compilation error private void display() { System.out.println("This is a private method in the subclass."); } } public class MethodOverridingDemo { public static void main(String[] args) { Superclass superObj = new Superclass(); Subclass subObj = new Subclass(); superObj.display(); subObj.display(); // This would not compile due to the access modifier change } }
attempting to assign weaker access privileges; was public
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions