Overriding Methods Example
This program demonstrates method overriding.
Problem Statement
Write a Java program to override display() method in subclass.
Source Code
| 1 | class Student { |
| 2 | void display(){ System.out.println("Student Display"); } |
| 3 | } |
| 4 | class CollegeStudent extends Student { |
| 5 | @Override |
| 6 | void display(){ System.out.println("College Student Display"); } |
| 7 | } |
| 8 | public class MethodOverriding { |
| 9 | public static void main(String[] args) { |
| 10 | System.out.println("Eduinq Method Overriding"); |
| 11 | Student s=new CollegeStudent(); |
| 12 | s.display(); |
| 13 | } |
| 14 | } |
Program Output
Eduinq Method Overriding College Student Display
Explanation
Method overriding allows subclass to provide specific implementation of a method defined in superclass. - At runtime, JVM decides which method to execute based on object type. - This enables polymorphism and dynamic method dispatch, a core OOP principle. Conclusion: Shows how to override methods in Java.