Using super Keyword
This program demonstrates usage of super keyword.
Problem Statement
Write a Java program to use super keyword to call superclass constructor and method.
Source Code
| 1 | class Person { |
| 2 | Person(){ System.out.println("Person constructor"); } |
| 3 | void show(){ System.out.println("Person show"); } |
| 4 | } |
| 5 | class Student extends Person { |
| 6 | Student(){ super(); System.out.println("Student constructor"); } |
| 7 | @Override |
| 8 | void show(){ |
| 9 | super.show(); |
| 10 | System.out.println("Student show"); |
| 11 | } |
| 12 | } |
| 13 | public class SuperKeyword { |
| 14 | public static void main(String[] args) { |
| 15 | System.out.println("Eduinq Super Keyword"); |
| 16 | Student s=new Student(); |
| 17 | s.show(); |
| 18 | } |
| 19 | } |
Program Output
Eduinq Super Keyword Person constructor Student constructor Person show Student show
Explanation
super() calls superclass constructor. - super.show() invokes superclass method. - This ensures proper initialization and allows subclass to extend behavior without losing parent functionality. Conclusion: Shows how to use super keyword in Java.