What does the super keyword represents and where can it be used? Give an example
ID: 3876501 • Letter: W
Question
What does the super keyword represents and where can it be used? Give an example of a superclass and subclass. Be sure to make all the instances variables of the super class private. Include at least one constructor in each class and ensure that the constructor of the subclass calls the constructor of the superclass. Also include a toString method in both classes that returns the values of the instance variables with appropriate labels. Ensure that the toString method of subclass calls the toString method of the superclass so that the string returned contains the values of all the inherited instance variables.
Explanation / Answer
super keyword is used in base class during inheritance. The word super represents the base class. It can be used to invoke base class methods. It is specifically used when the sub class overrides any methods defined in super class. Then to invoke the implementation in base class, we use super.methodname() to call the base class method. Also to invoke the base class constructor, we use super.
The following code shows how super keyword is used to call base class constructor and also override methods from base class and call them using super.getAnnualSalary() and super.toString()
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Employee.java
-==========
public class Employee {
private String name;
private double monthlySalary;
public Employee(String n, double monthlySal)
{
this.name = n;
this.monthlySalary = monthlySal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public double getAnnualSalary()
{
return 12 * monthlySalary;
}
public String toString()
{
return "Name: " + name + ", Monthly Salary: $" + monthlySalary;
}
}
Salesman.java
==============
public class Salesman extends Employee {
private double annualSales;
public Salesman(String n, double monthlySal, double annualSale)
{
super(n, monthlySal); //call base class constructor
this.annualSales = annualSale;
}
public double getAnnualSales() {
return annualSales;
}
public void setAnnualSales(double annualSales) {
this.annualSales = annualSales;
}
@Override
public double getAnnualSalary() {
// override base class method
//add extra commission of 5% of annual sales
return super.getAnnualSalary() + 0.05 * annualSales;
}
public String toString()
{
return super.toString() + ", Annual Sales = $" + annualSales;
}
}
TestEmployee.java
================
public class TestEmployee {
public static void main(String[] args) {
Employee e1 = new Employee("John", 2000);
Employee e2 = new Salesman("Bob", 1500, 25000);
System.out.println("Employee 1: " + e1);
System.out.println("Annual salary of employee 1: " + e1.getAnnualSalary());
System.out.println();
System.out.println("Employee 2: " + e2);
System.out.println("Annual salary of employee 2: " + e2.getAnnualSalary());
}
}
output
=======
Employee 1: Name: John, Monthly Salary: $2000.0
Annual salary of employee 1: 24000.0
Employee 2: Name: Bob, Monthly Salary: $1500.0, Annual Sales = $25000.0
Annual salary of employee 2: 19250.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.