Java Overriding

Overriding is a way to providing new definition of method which are already exist in parent class (superclass). This new method definition is consist of similar to the parent class method. Such as number of parameters, sequence of parameter data type. But functionality can be are different.

class Employee{
  //Employee class method
  double calculateSalary(int workingDay){
    return workingDay*1000.50; 
  } 

}
//Inherit the properties of Employee class
class SalesEmployee extends Employee{
  int productSales;
  SalesEmployee(int productSales){
    this.productSales=productSales;
  }
  //Override the Employee class method
  @Override
  double calculateSalary(int workingDay){
    double saleAmount = productSales*500 ;
    double actualSalary = workingDay*1200;
    return actualSalary + saleAmount;
  } 
  public static void main(String args[]){
    //create object of SalesEmployee
    SalesEmployee person =new SalesEmployee(10);
    double amount=person.calculateSalary(17);//Executes Derived class method

    System.out.println("Amount : "+amount);
  }
}

Output

Amount : 25400.0
Overriding hide the superclass method

Overriding are not remove the method of superclass. but that is hide that superclass method. We are know about how to use super class method in this situation. We can use super keyword to retrieve superclass method. Like that.

//workingDay is integer value
super.calculateSalary(workingDay);//using this within the derived class method

Overriding function of Object class

When we define a custom class then internal that are inherit the properties of Object class automatically. For example we can access the toString() and equals() method in anywhere within the program. But observe that we are implement this function definition. But capable to access the functionality of a Object class.

//Override toString() method of Object class
class Fruits{

  String type;
  int quantity;
  //set initial value of class properties
  Fruits (String type,int quantity){
    this.type=type;
    this.quantity=quantity;
  }

  //Override version of Object class toString() method
  @Override
  public String toString() 
  {
    //Return object information
    return "name = "+ type +", quantity = "+ quantity;
   
  }
  public static void main(String args[]){
    //Create class object
    Fruits apple = new Fruits("Apple",5);
    Fruits mango = new Fruits("Mango",10);
    //It will work on Fruits class object
    System.out.println(apple.toString());
    System.out.println(mango.toString());
  }
}

Output

Fruits Name = Apple, And Quantity = 5
Fruits Name = Mango, And Quantity = 10

In this example implemented new functionality to toString() method which are return the information about object properties.

Overriding the Object class method tostring()

That is an simplest example which are Override existing method functionality.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved