How to invoke super class version of Overridden method

How to invoke super class version of Overridden method

Answer : Use keyword super to invoke super class version of overridden methods. Please have example below:

package com.javahonk.overridenExample;

public class SoftwareEngineer extends Employee {

    public static void main(String[] args) {
	SoftwareEngineer softwareEngineer=new SoftwareEngineer();
	softwareEngineer.name();
	softwareEngineer.salary();	

    }

    @Override
    public void name() {
	//User super to invoke super class method
	super.name();
	System.out.println("Software person name: Java Honk");
    }

    @Override
    public void salary() {
	//User super to invoke super class method
	super.salary();
	System.out.println("Software Engineer Salary: $5000");
    }

}

class Employee {

    public void name() {
	System.out.println("Employee name: Java Honk");
    }

    public void salary() {
	System.out.println("Employee salary: $5000");
    }

}

 

Output:

Your name: Java Honk
Your change name: Java Monk

 

Leave a Reply

Your email address will not be published. Required fields are marked *